Skip to content

Instantly share code, notes, and snippets.

@jdennes
Last active April 3, 2023 15:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdennes/4e433d0c4189a9249da6cc89b5716b74 to your computer and use it in GitHub Desktop.
Save jdennes/4e433d0c4189a9249da6cc89b5716b74 to your computer and use it in GitHub Desktop.
Get the Rails controller and action given a path

Get the Rails controller and action from a given path/route

Given an application path like /github/github/issues, use the following to get the controller and action:

irb(main):001:0> Rails.application.routes.recognize_path "/github/github/issues", method: "GET"
=> {:controller=>"issues", :action=>"index", :user_id=>"github", :repository=>"github"}

The /github/github/issues path maps to issues#index (the index action on the IssuesController).

More examples:

irb(main):002:0> Rails.application.routes.recognize_path "/github/github", method: "GET"
=> {:format=>:html, :controller=>"files", :action=>"disambiguate", :user_id=>"github", :repository=>"github"}
irb(main):003:0> Rails.application.routes.recognize_path "/github", method: "GET"
=> {:controller=>"profiles", :action=>"show", :user_id=>"github"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment