Class: ActionController::Metal
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
Base, Middleware, Rails::ApplicationController, Rails::InfoController, Rails::MailersController, Rails::WelcomeController, ActionView::TestCase::TestController
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
AbstractController::Base
|
Defined in: | actionpack/lib/action_controller/metal.rb |
Overview
Metal
is the simplest possible controller, providing a valid ::Rack interface without the additional niceties provided by Base.
A sample metal controller might look like this:
class HelloController < ActionController::Metal
def index
self.response_body = "Hello World!"
end
end
And then to route requests to your metal controller, you would add something like this to config/routes.rb
:
get 'hello', to: HelloController.action(:index)
The .action method returns a valid ::Rack application for the Rails router to dispatch to.
Rendering Helpers
Metal
by default provides no utilities for rendering views, partials, or other responses aside from explicitly calling of #response_body=, #content_type=, and #status=. To add the render helpers you're used to having in a normal controller, you can do the following:
class HelloController < ActionController::Metal
include AbstractController::Rendering
include ActionView::Layouts
append_view_path "#{Rails.root}/app/views"
def index
render "hello/index"
end
end
Redirection Helpers
To add redirection helpers to your metal controller, do the following:
class HelloController < ActionController::Metal
include ActionController::Redirecting
include Rails.application.routes.url_helpers
def index
redirect_to root_url
end
end
Other Helpers
You can refer to the modules included in Base to see other features you can bring into your metal controller.
Class Attribute Summary
::AbstractController::Base - Inherited
.abstract? | Alias for AbstractController::Base.abstract. |
.supports_path? | Returns true if the given controller is capable of rendering a path. |
Class Method Summary
-
.action(name, klass = ActionDispatch::Request)
Returns a ::Rack endpoint for the given action name.
- .call(env)
-
.controller_name
Returns the last part of the controller's name, underscored, without the ending
Controller
. -
.middleware
Alias for .middleware_stack.
- .new ⇒ Metal (also: .build) constructor
-
.use(*args, &block)
Pushes the given ::Rack middleware and its arguments to the bottom of the middleware stack.
::AbstractController::Base - Inherited
.abstract, | |
.abstract! | Define a controller as abstract. |
.action_methods | A list of method names that should be considered actions. |
.clear_action_methods! | action_methods are cached and there is sometimes need to refresh them. |
.controller_path | Returns the full controller name, underscored, without the ending Controller. |
.hidden_actions | The list of hidden actions. |
.internal_methods | A list of all internal methods for a controller. |
.method_added | Refresh the cached action_methods when a new action_method is added. |
::ActiveSupport::DescendantsTracker - Extended
Instance Attribute Summary
- #content_type rw
-
#content_type=(type)
rw
Basic implementations for content_type=, location=, and headers are provided to reduce the dependency on the RackDelegation module in Renderer and Redirector.
- #env rw
- #env=(value) rw
-
#headers
rw
The details below can be overridden to support a specific Request and Response object.
- #location rw
- #location=(url) rw
- #middleware_stack rw
- #middleware_stack? ⇒ Boolean rw
- #params rw
- #params=(val) rw
-
#performed? ⇒ Boolean
readonly
Tests if render or redirect has already happened.
-
#request
rw
The details below can be overridden to support a specific Request and Response object.
-
#response
rw
The details below can be overridden to support a specific Request and Response object.
- #response_body=(body) writeonly
- #session readonly
- #status (also: #response_code) rw
- #status=(status) rw
::AbstractController::Base - Inherited
Instance Method Summary
-
#controller_name
Delegates to the class'
controller_name
. -
#url_for(string)
Basic url_for that can be overridden for more robust functionality.
::AbstractController::Base - Inherited
#action_methods | Delegates to the class' |
#available_action? | Returns true if a method for the action is available and can be dispatched, false otherwise. |
#controller_path | Delegates to the class' |
#process | Calls the action going through the entire action dispatch stack. |
::ActiveSupport::Configurable - Included
#config | Reads and writes attributes from a configuration |
Constructor Details
.new ⇒ Metal
Also known as: .build
# File 'actionpack/lib/action_controller/metal.rb', line 131
def initialize @_headers = {"Content-Type" => "text/html"} @_status = 200 @_request = nil @_response = nil @_routes = nil super end
Class Attribute Details
.middleware_stack (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 204
class_attribute :middleware_stack
.middleware_stack? ⇒ Boolean
(rw)
[ GitHub ]
# File 'actionpack/lib/action_controller/metal.rb', line 204
class_attribute :middleware_stack
Class Method Details
.action(name, klass = ActionDispatch::Request)
Returns a ::Rack endpoint for the given action name.
# File 'actionpack/lib/action_controller/metal.rb', line 231
def self.action(name, klass = ActionDispatch::Request) if middleware_stack.any? middleware_stack.build(name) do |env| new.dispatch(name, klass.new(env)) end else lambda { |env| new.dispatch(name, klass.new(env)) } end end
.call(env)
.controller_name
Returns the last part of the controller's name, underscored, without the ending Controller
. For instance, PostsController returns posts
. Namespaces are left out, so Admin::PostsController
returns posts
as well.
Returns
-
string
# File 'actionpack/lib/action_controller/metal.rb', line 113
def self.controller_name @controller_name ||= name.demodulize.sub(/Controller$/, '').underscore end
.middleware
Alias for .middleware_stack.
# File 'actionpack/lib/action_controller/metal.rb', line 219
def self.middleware middleware_stack end
.use(*args, &block)
Pushes the given ::Rack middleware and its arguments to the bottom of the middleware stack.
# File 'actionpack/lib/action_controller/metal.rb', line 214
def self.use(*args, &block) middleware_stack.use(*args, &block) end
Instance Attribute Details
#content_type (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 156
def content_type headers["Content-Type"] end
#content_type=(type) (rw)
Basic implementations for content_type=, location=, and headers are provided to reduce the dependency on the RackDelegation module in Renderer and Redirector.
# File 'actionpack/lib/action_controller/metal.rb', line 152
def content_type=(type) headers["Content-Type"] = type.to_s end
#env (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 103
def env @_env ||= {} end
#env=(value) (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 101
attr_internal_writer :env
#headers (rw)
The details below can be overridden to support a specific Request and Response object. The default Base implementation includes RackDelegation, which makes a request and response object available. You might wish to control the environment and response manually for performance reasons.
#location (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 160
def location headers["Location"] end
#location=(url) (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 164
def location=(url) headers["Location"] = url end
#middleware_stack (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 204
class_attribute :middleware_stack
#middleware_stack? ⇒ Boolean
(rw)
[ GitHub ]
# File 'actionpack/lib/action_controller/metal.rb', line 204
class_attribute :middleware_stack
#params (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 140
def params @_params ||= request.parameters end
#params=(val) (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 144
def params=(val) @_params = val end
#performed? ⇒ Boolean
(readonly)
Tests if render or redirect has already happened.
#request (rw)
The details below can be overridden to support a specific Request and Response object. The default Base implementation includes RackDelegation, which makes a request and response object available. You might wish to control the environment and response manually for performance reasons.
#response (rw)
The details below can be overridden to support a specific Request and Response object. The default Base implementation includes RackDelegation, which makes a request and response object available. You might wish to control the environment and response manually for performance reasons.
#response_body=(body) (writeonly)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 182
def response_body=(body) body = [body] unless body.nil? || body.respond_to?(:each) super end
#session (readonly)
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 129
delegate :session, :to => "@_request"
#status (rw) Also known as: #response_code
[ GitHub ]# File 'actionpack/lib/action_controller/metal.rb', line 173
def status @_status end
#status=(status) (rw)
[ GitHub ]Instance Method Details
#controller_name
Delegates to the class' controller_name
# File 'actionpack/lib/action_controller/metal.rb', line 118
def controller_name self.class.controller_name end
#url_for(string)
Basic url_for that can be overridden for more robust functionality
# File 'actionpack/lib/action_controller/metal.rb', line 169
def url_for(string) string end