class ActionController::Renderer
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/action_controller/renderer.rbs class ActionController::Renderer def rack_value_for: (Symbol key, String value) -> untyped end
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)
Experimental RBS support (using type sampling data from the type_fusion
project).
def rack_value_for: (Symbol key, String value) -> untyped
This signature was generated using 1 sample from 1 application.
def rack_value_for(key, value) case key when :https value ? "on" : "off" when :method -value.upcase else value end end
def render(*args)
passing in the current view context.
If an object responding to +render_in+ is passed, +render_in+ is called on the object,
If no options hash is passed or if :update is specified, then:
* :body - Renders provided text and sets content type of text/plain.
need to call .to_json on the object you want to render.
* :json - Renders the provided hash or object in JSON. You don't
performs HTML escape on the string first. Sets the content type as text/html.
* :html - Renders the provided HTML safe string, otherwise
* :plain - Renders provided text and sets the content type as text/plain.
* :inline - Renders an ERB template string.
It shouldn’t be used directly with unsanitized user input due to lack of validation.
* :file - Renders an explicit template file. Add :locals to pass in, if so desired.
* :partial - See ActionView::PartialRenderer for details.
The primary options are:
Render templates with any options from ActionController::Base#render_to_string.
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