class ActionController::Renderer
ApplicationController.renderer.new(method: ‘post’, https: true)
* by initializing an instance of renderer by passing it a custom environment.
ApplicationController.renderer.defaults # => hash with default Rack environment
* by changing renderer defaults, like
ActionController::Renderer#env. You can set it up in two ways:
The template will be rendered in a Rack environment which is accessible through
FooController.render :action, locals: { … }, assigns: { … }
For example:
#render allows you to use the same options that you can use when rendering in a controller.
ApplicationController.render template: ‘…’
You can use this shortcut in a controller, instead of the previous example:
ApplicationController.renderer.render template: ‘…’
It allows you to call method #render directly.
ApplicationController.renderer
For example:
You get a concrete renderer class by invoking ActionController::Base#renderer.
without requirement of being in controller actions.
ActionController::Renderer allows you to render arbitrary templates
def self.for(controller, env = {}, defaults = DEFAULTS.dup)
def self.for(controller, env = {}, defaults = DEFAULTS.dup) new(controller, env, defaults) end
def initialize(controller, env, defaults)
It will be merged with the default Rack environment defined by
Accepts a custom Rack environment to render templates in.
def initialize(controller, env, defaults) @controller = controller @defaults = defaults @env = normalize_keys defaults, env end
def new(env = {})
def new(env = {}) self.class.new controller, env, defaults end
def normalize_keys(defaults, env)
def normalize_keys(defaults, env) new_env = {} env.each_pair { |k, v| new_env[rack_key_for(k)] = rack_value_for(k, v) } defaults.each_pair do |k, v| key = rack_key_for(k) new_env[key] = rack_value_for(k, v) unless new_env.key?(key) end new_env["rack.url_scheme"] = new_env["HTTPS"] == "on" ? "https" : "http" new_env end
def rack_key_for(key)
def rack_key_for(key) RACK_KEY_TRANSLATION[key] || key.to_s end
def rack_value_for(key, value)
def rack_value_for(key, value) case key when :https value ? "on" : "off" when :method -value.upcase else value end end
def render(*args)
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
def with_defaults(defaults)
def with_defaults(defaults) self.class.new controller, @env, self.defaults.merge(defaults) end