我需要在Rails中的过滤器中知道当前路由。我怎么才能知道是什么?
我正在使用REST资源,没有看到命名路由。
我需要在Rails中的过滤器中知道当前路由。我怎么才能知道是什么?
我正在使用REST资源,没有看到命名路由。
当前回答
你可以这样做:
def active_action?(controller)
'active' if controller.remove('/') == controller_name
end
现在,你可以这样用:
<%= link_to users_path, class: "some-class #{active_action? users_path}" %>
其他回答
我找到了批准的答案,请求。env['PATH_INFO'],用于获取基本URL,但如果你有嵌套路由,它并不总是包含完整路径。你可以使用request。env['HTTP_REFERER']获取完整路径,然后查看它是否匹配给定的路由:
request.env['HTTP_REFERER'].match?(my_cool_path)
你可以这样做
Rails.application.routes.recognize_path "/your/path"
它在rails 3.1.0.rc4中为我工作
你可以这样做:
def active_action?(controller)
'active' if controller.remove('/') == controller_name
end
现在,你可以这样用:
<%= link_to users_path, class: "some-class #{active_action? users_path}" %>
如果你还需要这些参数:
current_fullpath = request.env['ORIGINAL_FULLPATH'] # If you are browsing http://example.com/my/test/path?param_n=N # then current_fullpath will point to "/my/test/path?param_n=N"
记住,你总是可以调用<%=调试请求。Env %>,以查看所有可用选项。
你可以请求。env['REQUEST_URI']查看完整的请求URI..它将输出如下内容
http://localhost:3000/client/1/users/1?name=test