Action Controller Renderer
ActionController::Renderer
allows you to render arbitrary templates without being inside a controller action.
You can get a renderer instance by calling renderer
on a controller class:
ApplicationController.renderer
PostsController.renderer
and render a template by calling the render
method:
ApplicationController.renderer.render template: "posts/show", assigns: { post: Post.first }
PostsController.renderer.render :show, assigns: { post: Post.first }
As a shortcut, you can also call render
directly on the controller class itself:
ApplicationController.render template: "posts/show", assigns: { post: Post.first }
PostsController.render :show, assigns: { post: Post.first }
Methods
Constants
DEFAULTS | = | { method: "get", input: "" }.freeze |
RACK_KEY_TRANSLATION | = | { http_host: "HTTP_HOST", https: "HTTPS", method: "REQUEST_METHOD", script_name: "SCRIPT_NAME", input: "rack.input" } |
Attributes
[R] | controller |
Class Public methods
for(controller, env = nil, defaults = DEFAULTS)
Creates a new renderer using the given controller class. See ::new
.
📝 Source code
# File actionpack/lib/action_controller/renderer.rb, line 64
def self.for(controller, env = nil, defaults = DEFAULTS)
new(controller, env, defaults)
end
🔎 See on GitHub
new(controller, env, defaults)
Initializes a new Renderer
.
Parameters
-
controller
- The controller class to instantiate for rendering. -
env
- The Rack env to use for mocking a request when rendering. Entries can be typical Rack env keys and values, or they can be any of the following, which will be converted appropriately:-
:http_host
- The HTTP host for the incoming request. Converts to Rack’sHTTP_HOST
. -
:https
- Boolean indicating whether the incoming request uses HTTPS. Converts to Rack’sHTTPS
. -
:method
- The HTTP method for the incoming request, case-insensitive. Converts to Rack’sREQUEST_METHOD
. -
:script_name
- The portion of the incoming request’s URL path that corresponds to the application. Converts to Rack’sSCRIPT_NAME
. -
:input
- The input stream. Converts to Rack’srack.input
. -
:http_host
- The HTTP host for the incoming request. Converts to Rack’sHTTP_HOST
. -
:https
- Boolean indicating whether the incoming request uses HTTPS. Converts to Rack’sHTTPS
. -
:method
- The HTTP method for the incoming request, case-insensitive. Converts to Rack’sREQUEST_METHOD
. -
:script_name
- The portion of the incoming request’s URL path that corresponds to the application. Converts to Rack’sSCRIPT_NAME
. -
:input
- The input stream. Converts to Rack’srack.input
.
-
-
defaults
- Default values for the Rack env. Entries are specified in the same format asenv
.env
will be merged on top of these values.defaults
will be retained when callingnew
on a renderer instance.
If no http_host
is specified, the env HTTP host will be derived from the routes’ default_url_options
. In this case, the https
boolean and the script_name
will also be derived from default_url_options
if they were not specified. Additionally, the https
boolean will fall back to Rails.application.config.force_ssl
if default_url_options
does not specify a protocol
.
📝 Source code
# File actionpack/lib/action_controller/renderer.rb, line 111
def initialize(controller, env, defaults)
@controller = controller
@defaults = defaults
if env.blank? && @defaults == DEFAULTS
@env = DEFAULT_ENV
else
@env = normalize_env(@defaults)
@env.merge!(normalize_env(env)) unless env.blank?
end
end
🔎 See on GitHub
Instance Public methods
defaults()
📝 Source code
# File actionpack/lib/action_controller/renderer.rb, line 122
def defaults
@defaults = @defaults.dup if @defaults.frozen?
@defaults
end
🔎 See on GitHub
new(env = nil)
Creates a new renderer using the same controller, but with a new Rack env.
ApplicationController.renderer.new(method: "post")
📝 Source code
# File actionpack/lib/action_controller/renderer.rb, line 72
def new(env = nil)
self.class.new controller, env, @defaults
end
🔎 See on GitHub
render(*args)
Renders a template to a string, just like ActionController::Rendering#render_to_string
.
📝 Source code
# File actionpack/lib/action_controller/renderer.rb, line 129
def render(*args)
request = ActionDispatch::Request.new(env_for_request)
request.routes = controller._routes
instance = controller.new
instance.set_request! request
instance.set_response! controller.make_response!(request)
instance.render_to_string(*args)
end
🔎 See on GitHub
with_defaults(defaults)
Creates a new renderer using the same controller, but with the given defaults merged on top of the previous defaults.
📝 Source code
# File actionpack/lib/action_controller/renderer.rb, line 78
def with_defaults(defaults)
self.class.new controller, @env, @defaults.merge(defaults)
end
🔎 See on GitHub