如何在rails中获取当前路线 [英] How to get the current route in rails

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

问题描述

我正在构建一些 Rails 示例应用程序,其中有两个模型 User 和 Projects.两者之间的关联是用户 has_many 项目.现在的问题是,我愿意在顶部为用户的项目提供一些下拉菜单,如果有人单击该项目,则会将其带到项目显示页面.但是,如果用户在一个项目的编辑页面并从下拉列表中选择另一个项目,我希望他到达新选定项目的编辑页面,以显示页面任何想法.我正在寻找的解决方案是例如:-我们可以使用 params[:controller] 找到当前控制器,使用 params[:action] 找到当前操作如何找到当前路线.提前谢谢

I am building some rails sample application in which I am having two models User and Projects. Association between both is user has_many projects. Now the question is I am willing to provide some drop down for user's projects on top and if some one click on that one it will take that to project show page. But if user is at edit page of one project and select other project from dropdown I want him to reach edit page of new selected project same case for show page any idea. The solution I am looking for is e.g:- we can find current controller using params[:controller], find current action using params[:action] how can I find current route. Thanx in advance

如果有人想看看我的代码是什么:- 这里是 github 的链接:-'https://github.com/peeyushsingla/session_test.git/ 这里我使用的是简单的链接,但实际上我会提供一些将显示的单个下拉列表,适用于所有编辑、显示、新操作

If someone want to see what is my code:- here is the link for github:- 'https://github.com/peeyushsingla/session_test.git/ Here I am using simple link but actually I will provide some single drop down that will be displayed that will work for all the edit, show, new every action

推荐答案

要检索 URL:

# Current URL:
request.original_url

# Current relative URL:
request.request_uri

另外,您可以使用current_page方法检查您是否在某个路线上.

Additionally, you can check with the current_page method whether you are in a certain route.

current_page?(my_path)
# => true / false

对于 Rails 4 使用:

# Current URL:
request.original_url

# Current relative URL:
request.fullpath

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

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