luca luca - 11 months ago 215
Ruby Question

How can I find out the current route in Rails?

I need to know the current route in a filter in Rails. How can I find out what it is?

I'm doing REST resources, and see no named routes.


To find out URI:

current_uri = request.env['PATH_INFO']
# If you are browsing, 
# then above line will yield current_uri as "/my/test/path"

To find out the route i.e. controller, action and params:

path = ActionController::Routing::Routes.recognize_path "/your/path/here/"

# ...or newer Rails versions:
path = Rails.application.routes.recognize_path('/your/path/here')

controller = path[:controller]
action = path[:action]
# You will most certainly know that params are available in 'params' hash