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.
render partial: "posts/form", locals: < post: Post.new ># => renders app/views/posts/_form.html.erb
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
Renders an ERB template string.
@name = "World" render inline: "Hello, !
" # => renders "Hello, World!
"
Renders the provided text, and sets the content type as text/plain .
render body: "Hello, World!" # => renders "Hello, World!"
Renders the provided text, and sets the content type as text/plain .
render plain: "Hello, World!" # => renders "Hello, World!"
Renders the provided HTML string, and sets the content type as text/html . If the string is not html_safe? , performs HTML escaping on the string before rendering.
render html: "Hello, World!
".html_safe # => renders "Hello, World!
" render html: "Hello, World!
" # => renders "<h1>Hello, World!</h1>"
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 calling to_json .
render json: < hello: "world" ># => renders ""
By default, when a rendering mode is specified, no layout template is rendered.
Hash of instance variable assignments for the template.
render inline: "Hello, !
", assigns: < name: "World" ># => renders "Hello, World!
"
Hash of local variable assignments for the template.
render inline: "Hello, !
", locals: < name: "World" ># => renders "Hello, World!
"
The layout template to render. Can also be false or true 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: "Hello, World!
", layout: true # => renders "Hello, World!
" with the default layout
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
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