Constants
| RENDER_FORMATS_IN_PRIORITY | = | [:body, :plain, :html] |
Instance Public methods
render(*args) Link
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.
class Greeting
def render_in(view_context)
view_context.render html: "<h1>Hello, World</h1>"
end
def format
:html
end
end
render(Greeting.new)
# => "<h1>Hello, World</h1>"
render(renderable: Greeting.new)
# => "<h1>Hello, World</h1>"
Rendering Mode
:partial-
See
ActionView::PartialRendererfor 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
ERBtemplate 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\"}"
:renderable-
Renders the provided object by calling
render_inwith the current view context. The response format is determined by callingformaton the renderable if it responds toformat, falling back totext/htmlby default.
render renderable: Greeting.new
# => renders "<h1>Hello, World</h1>"
By default, when a rendering mode is specified, no layout template is rendered.
Options
:assigns-
Hashof instance variable assignments for the template.
render inline: "<h1>Hello, <%= @name %>!</h1>", assigns: { name: "World" }
# => renders "<h1>Hello, World!</h1>"
:locals-
Hashof 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
falseortrueto 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
Symbolform. 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