instance method controller_class_for

Ruby on Rails edge

Since v5.1.7

Available in: v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Signature

controller_class_for(name)

No documentation comment.

Parameters

name req
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

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close