我需要在Rails的过滤器中了解当前路线。我怎么知道它是什么?

我正在做REST资源,看不到命名路由。

最佳答案

要查找URI:

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

要找出路线,即 Controller , Action 和参数:
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

关于ruby-on-rails - 如何在Rails中找到当前路线?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1203892/

10-10 21:24
查看更多