在我的 routes.rb 文件中,我有许多路线。我想知道哪一个与用户可能提出的任何请求相匹配。
例如,在 routes.rb 中,您有以下路线:
map.connect ":controller/:action" map.connect ":controler/:action/:id"
如果我有一个名为a 的 Controller 和一个名为first 的操作,当用户转到 http://whatever/a/first 时 ,我希望看到选择了 :controller/:action 路由。
我可以为任何请求这样做吗?
最佳答案
如果你只是想知道你在哪个 Controller / Action 中, Controller 提供了controller_name
和action_name
方法。所以你可以这样做:
# 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>
关于ruby-on-rails - 有什么方法可以查看哪条路线与 Rails 中浏览器的请求相匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/811314/