Class: ActionController::Renderer
Relationships & Source Files | |
Inherits: | Object |
Defined in: | actionpack/lib/action_controller/renderer.rb |
Overview
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:
Constant Summary
-
DEFAULTS =
# File 'actionpack/lib/action_controller/renderer.rb', line 41{ http_host: "example.org", https: false, method: "get", script_name: "", input: "" }.freeze
-
IDENTITY =
# File 'actionpack/lib/action_controller/renderer.rb', line 102->(_) { _ }
-
RACK_KEY_TRANSLATION =
# File 'actionpack/lib/action_controller/renderer.rb', line 94{ http_host: "HTTP_HOST", https: "HTTPS", method: "REQUEST_METHOD", script_name: "SCRIPT_NAME", input: "rack.input" }
-
RACK_VALUE_TRANSLATION =
# File 'actionpack/lib/action_controller/renderer.rb', line 104{ https: ->(v) { v ? "on" : "off" }, method: ->(v) { v.upcase }, }
Class Method Summary
-
.for(controller, env = {}, defaults = DEFAULTS.dup)
Create a new renderer instance for a specific controller class.
-
.new(controller, env, defaults) ⇒ Renderer
constructor
Accepts a custom
::Rack
environment to render templates in.
Instance Attribute Summary
- #controller readonly
- #defaults readonly
Instance Method Summary
-
#new(env = {})
Create a new renderer for the same controller but with a new env.
-
#render(*args)
Render templates with any options from
ActionController::Base#render_to_string
. -
#with_defaults(defaults)
Create a new renderer for the same controller but with new defaults.
Constructor Details
.new(controller, env, defaults) ⇒ Renderer
# File 'actionpack/lib/action_controller/renderer.rb', line 67
def initialize(controller, env, defaults) @controller = controller @defaults = defaults @env = normalize_keys defaults.merge(env) end
Class Method Details
.for(controller, env = {}, defaults = DEFAULTS.dup)
Create a new renderer instance for a specific controller class.
# File 'actionpack/lib/action_controller/renderer.rb', line 50
def self.for(controller, env = {}, defaults = DEFAULTS.dup) new(controller, env, defaults) end
Instance Attribute Details
#controller (readonly)
[ GitHub ]# File 'actionpack/lib/action_controller/renderer.rb', line 39
attr_reader :defaults, :controller
#defaults (readonly)
[ GitHub ]# File 'actionpack/lib/action_controller/renderer.rb', line 39
attr_reader :defaults, :controller
Instance Method Details
#new(env = {})
Create a new renderer for the same controller but with a new env.
# File 'actionpack/lib/action_controller/renderer.rb', line 55
def new(env = {}) self.class.new controller, env, defaults end
#render(*args)
Render templates with any options from ActionController::Base#render_to_string
.
# File 'actionpack/lib/action_controller/renderer.rb', line 74
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
#with_defaults(defaults)
Create a new renderer for the same controller but with new defaults.
# File 'actionpack/lib/action_controller/renderer.rb', line 60
def with_defaults(defaults) self.class.new controller, @env, self.defaults.merge(defaults) end