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
= Action Controller Metal
def self.action(name)
def self.action(name) app = lambda { |env| req = ActionDispatch::Request.new(env) res = make_response! req new.dispatch(name, req, res) } if middleware_stack.any? middleware_stack.build(name, app) else app end end
def self.action_encoding_template(action) # :nodoc:
def self.action_encoding_template(action) # :nodoc: false 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.delete_suffix("Controller").underscore unless anonymous?) end
def self.dispatch(name, req, res)
Direct dispatch to the controller. Instantiates the controller, then
def self.dispatch(name, req, res) if middleware_stack.any? middleware_stack.build(name) { |env| new.dispatch(name, req, res) }.call req.env else new.dispatch(name, req, res) end end
def self.make_response!(request)
def self.make_response!(request) ActionDispatch::Response.new.tap do |res| res.request = request end end
def self.middleware
stack}[https://guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack]
Read more about {Rails middleware
end
use AuthenticationMiddleware, except: [:index, :show]
class PostsController < ApplicationController
allows for the following syntax:
By default uses a variation of ActionDispatch::MiddlewareStack which
The middleware stack used by this controller.
def self.middleware middleware_stack end
def controller_name
def controller_name self.class.controller_name end
def dispatch(name, request, response) # :nodoc:
def dispatch(name, request, response) # :nodoc: set_request!(request) set_response!(response) process(name) request.commit_flash to_a end
def inherited(subclass)
def inherited(subclass) super subclass.middleware_stack = middleware_stack.dup subclass.class_eval do @controller_name = nil end end
def initialize
def initialize @_request = nil @_response = nil @_response_body = nil @_routes = nil @_params = nil super 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.committed? end
def reset_session
def reset_session @_request.reset_session end
def response=(response)
def response=(response) set_response!(response) # Force `performed?` to return true: @_response_body = true end
def response_body=(body)
def response_body=(body) if body body = [body] if body.is_a?(String) response.body = body super else response.reset_body! end end
def set_request!(request) # :nodoc:
def set_request!(request) # :nodoc: @_request = request @_request.controller_instance = self end
def set_response!(response) # :nodoc:
def set_response!(response) # :nodoc: if @_response _, _, body = @_response body.close if body.respond_to?(:close) end @_response = response end
def to_a # :nodoc:
def to_a # :nodoc: response.to_a end
def url_for(string)
def url_for(string) string end
def use(...)
Pushes the given Rack middleware and its arguments to the bottom of the
def use(...) middleware_stack.use(...) end