class ActionController::Metal
other features you can bring into your metal controller.
You can refer to the modules included in ActionController::Base
to see
== Other Helpers
end
end
redirect_to root_url
def index
include Rails.application.routes.url_helpers
include ActionController::Redirecting
class HelloController < ActionController::Metal
To add redirection helpers to your metal controller, do the following:
== Redirection Helpers
end
end
render “hello/index”
def index
append_view_path “#{Rails.root}/app/views”
include ActionController::Rendering
class HelloController < ActionController::Metal
can do the following:
add the render helpers you’re used to having in a normal controller, youresponse_body=
, content_type=
, and status=
. To
views, partials, or other responses aside from explicitly calling ofActionController::Metal
by default provides no utilities for rendering
== Rendering Helpers
router to dispatch to.
The action
method returns a valid Rack application for the Rails
match ‘hello’, :to => HelloController.action(:index)
something like this to config/routes.rb
:
And then to route requests to your metal controller, you would add
end
end
self.response_body = “Hello World!”
def index
class HelloController < ActionController::Metal
A sample metal controller might look like this:ActionController::Base
.
valid Rack interface without the additional niceties provided byActionController::Metal
is the simplest possible controller, providing a
def self.action(name, klass = ActionDispatch::Request)
==== Returns
* action - An action name
==== Parameters
for the same action.
multiple calls into MyController.action will return the same object
Return a rack endpoint for the given action. Memoize the endpoint, so
def self.action(name, klass = ActionDispatch::Request) middleware_stack.build(name.to_s) do |env| new.dispatch(name, klass.new(env)) end end
def self.call(env)
Makes the controller a rack endpoint that points to the action in
def self.call(env) action(env['action_dispatch.request.path_parameters'][:action]).call(env) end
def self.controller_name
==== Returns
Namespaces are left out, so Admin::PostsController returns posts as well.
Controller. For instance, PostsController returns posts.
Returns the last part of the controller's name, underscored, without the ending
def self.controller_name @controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore end
def self.inherited(base) #nodoc:
def self.inherited(base) #nodoc: base.middleware_stack = self.middleware_stack.dup super end
def self.middleware
def self.middleware middleware_stack end
def self.use(*args, &block)
def self.use(*args, &block) middleware_stack.use(*args, &block) end
def content_type
def content_type headers["Content-Type"] end
def content_type=(type)
def content_type=(type) headers["Content-Type"] = type.to_s end
def controller_name
def controller_name self.class.controller_name end
def dispatch(name, request) #:nodoc:
def dispatch(name, request) #:nodoc: @_request = request @_env = request.env @_env['action_controller.instance'] = self process(name) to_a end
def env
def env @_env ||= {} end
def initialize
def initialize @_headers = {"Content-Type" => "text/html"} @_status = 200 @_request = nil @_response = nil @_routes = nil super end
def location
def location headers["Location"] end
def location=(url)
def location=(url) headers["Location"] = url end
def params
def params @_params ||= request.parameters end
def params=(val)
def params=(val) @_params = val end
def performed?
def performed? response_body end
def response_body=(val)
def response_body=(val) body = if val.is_a?(String) [val] elsif val.nil? || val.respond_to?(:each) val else [val] end super body end
def status
def status @_status end
def status=(status)
def status=(status) @_status = Rack::Utils.status_code(status) end
def to_a #:nodoc:
def to_a #:nodoc: response ? response.to_a : [status, headers, response_body] end
def url_for(string)
def url_for(string) string end