如何找到Rails中的当前路线? [英] How can I find out the current route in Rails?

查看:91
本文介绍了如何找到Rails中的当前路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

要查找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"

要找出路线,即控制器,动作和参数:

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

这篇关于如何找到Rails中的当前路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆