class ActionController::Renderer

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:

Constants

DEFAULTS
RACK_KEY_TRANSLATION

Attributes

controller[R]
defaults[R]

Public Class Methods

for(controller, env = {}, defaults = DEFAULTS.dup) click to toggle source

Create a new renderer instance for a specific controller class.

# File lib/action_controller/renderer.rb, line 48
def self.for(controller, env = {}, defaults = DEFAULTS.dup)
  new(controller, env, defaults)
end
new(controller, env, defaults) click to toggle source

Accepts a custom Rack environment to render templates in. It will be merged with the default Rack environment defined by ActionController::Renderer::DEFAULTS.

# File lib/action_controller/renderer.rb, line 65
def initialize(controller, env, defaults)
  @controller = controller
  @defaults = defaults
  @env = normalize_keys defaults, env
end

Public Instance Methods

new(env = {}) click to toggle source

Create a new renderer for the same controller but with a new env.

# File lib/action_controller/renderer.rb, line 53
def new(env = {})
  self.class.new controller, env, defaults
end
render(*args) click to toggle source

Render templates with any options from ActionController::Base#render_to_string.

The primary options are:

  • :partial - See ActionView::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 an ERB template string.

  • :plain - Renders provided text and sets the content type as text/plain.

  • :html - Renders the provided HTML safe string, otherwise performs HTML escape on the string first. Sets the content type as text/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 of text/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 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
Also aliased as: render_to_string
render_to_string(*args)
Alias for: render
with_defaults(defaults) click to toggle source

Create a new renderer for the same controller but with new defaults.

# File lib/action_controller/renderer.rb, line 58
def with_defaults(defaults)
  self.class.new controller, @env, self.defaults.merge(defaults)
end

Private Instance Methods

normalize_keys(defaults, env) click to toggle source
# File lib/action_controller/renderer.rb, line 105
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
rack_key_for(key) click to toggle source
# File lib/action_controller/renderer.rb, line 126
def rack_key_for(key)
  RACK_KEY_TRANSLATION[key] || key.to_s
end
rack_value_for(key, value) click to toggle source
# File lib/action_controller/renderer.rb, line 130
def rack_value_for(key, value)
  case key
  when :https
    value ? "on" : "off"
  when :method
    -value.upcase
  else
    value
  end
end