Module: ActionController::Rendering
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::Concern
|
|
Defined in: | actionpack/lib/action_controller/metal/rendering.rb |
Constant Summary
-
RENDER_FORMATS_IN_PRIORITY =
# File 'actionpack/lib/action_controller/metal/rendering.rb', line 7[:body, :plain, :html]
Class Method Summary
::ActiveSupport::Concern
- Extended
class_methods | Define class methods from given block. |
included | Evaluate given block in context of base class, so that you can write class macros here. |
prepended | Evaluate given block in context of base class, so that you can write class macros here. |
append_features, prepend_features |
Instance Method Summary
-
#render(*args)
Renders a template and assigns the result to
self.response_body
. -
#render_to_string
Similar to #render, but only returns the rendered template as a string, instead of setting
self.response_body
. -
#_normalize_options(options)
private
Normalize both text and status options.
- #_normalize_text(options) private
-
#_process_options(options)
private
Process controller specific options, as status, content-type and location.
- #_process_variant(options) private
- #_render_in_priorities(options) private
- #_set_html_content_type private
- #_set_rendered_content_type(format) private
- #_set_vary_header private
- #render_to_body(options = {}) Internal use only
-
#process_action
private
Internal use only
Before processing, set the request formats in current controller formats.
Instance Method Details
#_normalize_options(options) (private)
Normalize both text and status options.
# File 'actionpack/lib/action_controller/metal/rendering.rb', line 199
def ( ) _normalize_text( ) if [:html] [:html] = ERB::Util.html_escape( [:html]) end if [:status] [:status] = Rack::Utils.status_code( [:status]) end super end
#_normalize_text(options) (private)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/rendering.rb', line 213
def _normalize_text( ) RENDER_FORMATS_IN_PRIORITY.each do |format| if .key?(format) && [format].respond_to?(:to_text) [format] = [format].to_text end end end
#_process_options(options) (private)
Process controller specific options, as status, content-type and location.
# File 'actionpack/lib/action_controller/metal/rendering.rb', line 222
def ( ) status, content_type, location = .values_at(:status, :content_type, :location) self.status = status if status self.content_type = content_type if content_type headers["Location"] = url_for(location) if location super end
#_process_variant(options) (private)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/rendering.rb', line 168
def _process_variant( ) if defined?(request) && !request.nil? && request.variant.present? [:variant] = request.variant end end
#_render_in_priorities(options) (private)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/rendering.rb', line 174
def _render_in_priorities( ) RENDER_FORMATS_IN_PRIORITY.each do |format| return [format] if .key?(format) end nil end
#_set_html_content_type (private)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/rendering.rb', line 182
def _set_html_content_type self.content_type = Mime[:html].to_s end
#_set_rendered_content_type(format) (private)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/rendering.rb', line 186
def _set_rendered_content_type(format) if format && !response.media_type self.content_type = format.to_s end end
#_set_vary_header (private)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/rendering.rb', line 192
def _set_vary_header if response.headers["Vary"].blank? && request.should_apply_vary_header? response.headers["Vary"] = "Accept" end end
#process_action (private)
Before processing, set the request formats in current controller formats.
# File 'actionpack/lib/action_controller/metal/rendering.rb', line 163
def process_action(*) # :nodoc: self.formats = request.formats.filter_map(&:ref) super end
#render(*args)
Renders a template and assigns the result to self.response_body
.
If no rendering mode option is specified, the template will be derived from the first argument.
render "posts/show"
# => renders app/views/posts/show.html.erb
# In a PostsController action...
render :show
# => renders app/views/posts/show.html.erb
If the first argument responds to render_in
, the template will be rendered by calling render_in
with the current view context.
Rendering Mode
:partial
-
See ActionView::PartialRenderer for details.
render partial: "posts/form", locals: { post: Post.new } # => renders app/views/posts/_form.html.erb
:file
-
Renders the contents of a file. This option should not be used with unsanitized user input.
render file: "/path/to/some/file" # => renders /path/to/some/file
:inline
-
Renders an ERB template string.
@name = "World" render inline: "<h1>Hello, <%= @name %>!</h1>" # => renders "<h1>Hello, World!</h1>"
:body
-
Renders the provided text, and sets the content type as
text/plain
.render body: "Hello, World!" # => renders "Hello, World!"
:plain
-
Renders the provided text, and sets the content type as
text/plain
.render plain: "Hello, World!" # => renders "Hello, World!"
:html
-
Renders the provided HTML string, and sets the content type as
text/html
. If the string is nothtml_safe?
, performs HTML escaping on the string before rendering.render html: "<h1>Hello, World!</h1>".html_safe # => renders "<h1>Hello, World!</h1>" render html: "<h1>Hello, World!</h1>" # => renders "<h1>Hello, World!</h1>"
:json
-
Renders the provided object as JSON, and sets the content type as
application/json
. If the object is not a string, it will be converted to JSON by callingto_json
.render json: { hello: "world" } # => renders "{\"hello\":\"world\"}"
By default, when a rendering mode is specified, no layout template is rendered.
Options
:assigns
-
Hash of instance variable assignments for the template.
render inline: "<h1>Hello, <%= @name %>!</h1>", assigns: { name: "World" } # => renders "<h1>Hello, World!</h1>"
:locals
-
Hash of local variable assignments for the template.
render inline: "<h1>Hello, <%= name %>!</h1>", locals: { name: "World" } # => renders "<h1>Hello, World!</h1>"
:layout
-
The layout template to render. Can also be
false
ortrue
to disable or (re)enable the default layout template.render "posts/show", layout: "holiday" # => renders app/views/posts/show.html.erb with the app/views/layouts/holiday.html.erb layout render "posts/show", layout: false # => renders app/views/posts/show.html.erb with no layout render inline: "<h1>Hello, World!</h1>", layout: true # => renders "<h1>Hello, World!</h1>" with the default layout
:status
-
The HTTP status code to send with the response. Can be specified as a number or as the status name in Symbol form. Defaults to 200.
render "posts/new", status: 422 # => renders app/views/posts/new.html.erb with HTTP status code 422 render "posts/new", status: :unprocessable_entity # => renders app/views/posts/new.html.erb with HTTP status code 422
# File 'actionpack/lib/action_controller/metal/rendering.rb', line 137
def render(*args) raise ::AbstractController::DoubleRenderError if response_body super end
#render_to_body(options = {})
# File 'actionpack/lib/action_controller/metal/rendering.rb', line 157
def render_to_body( = {}) # :nodoc: super || _render_in_priorities( ) || " " end
#render_to_string
Similar to #render, but only returns the rendered template as a string, instead of setting self.response_body
.
# File 'actionpack/lib/action_controller/metal/rendering.rb', line 146
def render_to_string(*) result = super if result.respond_to?(:each) string = +"" result.each { |r| string << r } string else result end end