Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Latest commit

 

History

History
33 lines (25 loc) · 1.21 KB

find_controller_action_from_uri.md

File metadata and controls

33 lines (25 loc) · 1.21 KB

Find a controller and action from a URI string

Got GET /posts, want to craft a redirect_to based off of that URI and some other parameters?

Today I learned that you can extract out the controller and action for a particular route with the magical ActionDispatch::Routing::RouteSet#recognize_path, which can be called through Rails.application.routes.recognize_path.

Warning: #recognize_path isn't part of Rails' user facing public API. Do not use in production - its behavior might change without notice.

So given a routes file:

# config/routes.rb
# ...
resources :posts
# ...

And jumping into the rails console:

$ bundle exec rails console

Try out the following:

Rails.application.routes.recognize_path('/posts') #=> { controller: 'posts', action: 'index' }

I discovered this method via a failed refactoring, so I honestly don't have a great use-case for #recognize_path.

Nevertheless, I think #recognize_path is an interesting, undocumented, and huge part ActionDispatch::Routing::RouteSet's API!