class ActionController::Renderer
PostsController.render :show, assigns: { post: Post.first }
ApplicationController.render template: “posts/show”, assigns: { post: Post.first }
itself:
As a shortcut, you can also call ‘render` directly on the controller class
PostsController.renderer.render :show, assigns: { post: Post.first }
ApplicationController.renderer.render template: “posts/show”, assigns: { post: Post.first }
and render a template by calling the #render method:
PostsController.renderer
ApplicationController.renderer
You can get a renderer instance by calling `renderer` on a controller class:
being inside a controller action.
ActionController::Renderer allows you to render arbitrary templates without
# Action Controller Renderer
def self.for(controller, env = nil, defaults = DEFAULTS)
def self.for(controller, env = nil, defaults = DEFAULTS) new(controller, env, defaults) end
def self.normalize_env(env) # :nodoc:
def self.normalize_env(env) # :nodoc: new_env = {} env.each_pair do |key, value| case key when :https value = value ? "on" : "off" when :method value = -value.upcase end key = RACK_KEY_TRANSLATION[key] || key.to_s new_env[key] = value end if new_env["HTTP_HOST"] new_env["HTTPS"] ||= "off" new_env["SCRIPT_NAME"] ||= "" end if new_env["HTTPS"] new_env["rack.url_scheme"] = new_env["HTTPS"] == "on" ? "https" : "http" end new_env end
def defaults
def defaults @defaults = @defaults.dup if @defaults.frozen? @defaults end
def env_for_request
def env_for_request if @env.key?("HTTP_HOST") || controller._routes.nil? @env.dup else controller._routes.default_env.merge(@env) end end
def initialize(controller, env, defaults)
`Rails.application.config.force_ssl` if `default_url_options` does not specify
specified. Additionally, the `https` boolean will fall back to
`script_name` will also be derived from `default_url_options` if they were not
routes' `default_url_options`. In this case, the `https` boolean and the
If no `http_host` is specified, the env HTTP host will be derived from the
`defaults` will be retained when calling #new on a renderer instance.
same format as `env`. `env` will be merged on top of these values.
* `defaults` - Default values for the Rack env. Entries are specified in the
* `:input` - The input stream. Converts to Rack's `rack.input`.
corresponds to the application. Converts to Rack's `SCRIPT_NAME`.
* `:script_name` - The portion of the incoming request's URL path that
case-insensitive. Converts to Rack's `REQUEST_METHOD`.
* `:method` - The HTTP method for the incoming request,
Converts to Rack's `HTTPS`.
* `:https` - Boolean indicating whether the incoming request uses HTTPS.
Rack's `HTTP_HOST`.
* `:http_host` - The HTTP host for the incoming request. Converts to
following, which will be converted appropriately:
can be typical Rack env keys and values, or they can be any of the
* `env` - The Rack env to use for mocking a request when rendering. Entries
* `controller` - The controller class to instantiate for rendering.
#### Parameters
Initializes a new Renderer.
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
def new(env = nil)
ApplicationController.renderer.new(method: "post")
Creates a new renderer using the same controller, but with a new Rack env.
def new(env = nil) self.class.new controller, env, @defaults end
def render(*args)
Renders a template to a string, just like
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
def with_defaults(defaults)
Creates a new renderer using the same controller, but with the given defaults
def with_defaults(defaults) self.class.new controller, @env, @defaults.merge(defaults) end