instance method
controller_class_for
Ruby on Rails edge
Since v5.1.7Signature
controller_class_for(name)
No documentation comment.
Parameters
-
namereq
Source
# File actionpack/lib/action_dispatch/http/request.rb, line 181
def controller_class_for(name)
unless name
raise MissingController, "No :controller in path parameters; the request has not been routed to a controller"
end
# Controller param => constant name, e.g. "posts" => "PostsController".
# The param encoding checks make this run several times per request, so
# the derived name is cached per param. Caching the string rather than
# the constant keeps it reload-safe: constantize resolves afresh every
# time. Only names that resolved are cached, so the cache is bounded by
# the controllers that exist.
const_names = ActiveSupport::Ractors.store_if_absent(:action_dispatch_controller_const_names) { Concurrent::Map.new }
unless const_name = const_names[name]
const_name = "#{name.underscore.camelize}Controller".freeze
cache_const_name = true
end
begin
controller_class = const_name.constantize
rescue NameError => error
if error.missing_name == const_name || const_name.start_with?("#{error.missing_name}::")
raise MissingController.new(error.message, error.name)
else
raise
end
end
const_names[-name] = const_name if cache_const_name
controller_class
end
Defined in actionpack/lib/action_dispatch/http/request.rb line 181
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionDispatch::Request