ACM@UIUC TIL logo ACM@UIUC TIL

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!