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 ActionView::Layouts
include AbstractController::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
get ‘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)
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
def self.call(env)
Makes the controller a Rack endpoint that runs the action in the given
def self.call(env) req = ActionDispatch::Request.new env action(req.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 ||= name.demodulize.sub(/Controller$/, '').underscore end
def self.inherited(base) # :nodoc:
def self.inherited(base) # :nodoc: base.middleware_stack = middleware_stack.dup super end
def self.middleware
def self.middleware middleware_stack end
def self.use(*args, &block)
Pushes the given Rack middleware and its arguments to the bottom of the
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 || (response && response.committed?) end
def response_body=(body)
def response_body=(body) body = [body] unless body.nil? || body.respond_to?(:each) super 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