ActionController::Renderer
allows you to render arbitrary templates without requirement of being in controller actions.
You get a concrete renderer class by invoking ActionController::Base#renderer. For example:
ApplicationController.renderer
It allows you to call method render
directly.
ApplicationController.renderer.render template: '...'
You can use this shortcut in a controller, instead of the previous example:
ApplicationController.render template: '...'
render
allows you to use the same options that you can use when rendering in a controller. For example:
FooController.render :action, locals: { ... }, assigns: { ... }
The template will be rendered in a Rack environment which is accessible through ActionController::Renderer#env. You can set it up in two ways:
-
by changing renderer defaults, like
ApplicationController.renderer.defaults # => hash with default Rack environment
-
by initializing an instance of renderer by passing it a custom environment.
ApplicationController.renderer.new(method: 'post', https: true)
- F
- N
- R
- W
Constants
DEFAULTS | = | { http_host: "example.org", https: false, method: "get", script_name: "", input: "" }.freeze |
RACK_KEY_TRANSLATION | = | { http_host: "HTTP_HOST", https: "HTTPS", method: "REQUEST_METHOD", script_name: "SCRIPT_NAME", input: "rack.input" } |
Attributes
[R] | controller | |
[R] | defaults |
Class Public methods
for(controller, env = {}, defaults = DEFAULTS.dup) Link
Create a new renderer instance for a specific controller class.
new(controller, env, defaults) Link
Accepts a custom Rack environment to render templates in. It will be merged with the default Rack environment defined by ActionController::Renderer::DEFAULTS
.
Instance Public methods
new(env = {}) Link
Create a new renderer for the same controller but with a new env.
render(*args) Link
Render templates with any options from ActionController::Base#render_to_string.
The primary options are:
-
:partial
- SeeActionView::PartialRenderer
for details. -
:file
- Renders an explicit template file. Add:locals
to pass in, if so desired. It shouldn’t be used directly with unsanitized user input due to lack of validation. -
:inline
- Renders anERB
template string. -
:plain
- Renders provided text and sets the content type astext/plain
. -
:html
- Renders the provided HTML safe string, otherwise performs HTML escape on the string first. Sets the content type astext/html
. -
:json
- Renders the provided hash or object in JSON. You don't need to call.to_json
on the object you want to render. -
:body
- Renders provided text and sets content type oftext/plain
.
If no options
hash is passed or if :update
is specified, then:
If an object responding to render_in
is passed, render_in
is called on the object, passing in the current view context.
Otherwise, a partial is rendered using the second parameter as the locals hash.
# File actionpack/lib/action_controller/renderer.rb, line 91 def render(*args) raise "missing controller" unless controller request = ActionDispatch::Request.new @env request.routes = controller._routes instance = controller.new instance.set_request! request instance.set_response! controller.make_response!(request) instance.render_to_string(*args) end