is there any way to see which route matched a request from the browser in Rails?
-
03-07-2019 - |
Question
In my routes.rb file I have a number of routes. I would like to know which one got matched for any request a user may have made.
For example, in routes.rb you have the following routes :
map.connect ":controller/:action" map.connect ":controler/:action/:id"
and if I have a controller named a and an action named first, when a user goes to http://whatever/a/first , I would like to see that the :controller/:action route was picked.
Can I do that for any request?
Solution
If you just want to know which controller/action you're in, the controller provides controller_name
and action_name
methods. So you might do something like this:
# in application_controller.rb
before_filter :set_where_am_i
def set_where_am_i
@where_am_i = "#{controller_name}/#{action_name}"
end
# in views/layouts/application.erb, somewhere in your html
# (here I choose the <title>)
<head><title>Here: <%= @where_am_i %></title>
OTHER TIPS
The closest thing I could find was in the log files. You will see for each request which action of which controller matched. You will see something like this :
Parameters: {"action"=>"show", "id"=>"2", "controller"=>"mycontroller"}
From there, it should be easy,right?