Ruby-on-rails 如何在rails中获取当前路线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20181216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:03:56  来源:igfitidea点击:

How to get the current route in rails

ruby-on-railsrubyruby-on-rails-3rails-routing

提问by peeyush singla

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

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

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

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

回答by Abel

For Rails 4

对于 Rails 4

URL example: http://localhost:3000/allreferred?&referred_name=maria

网址示例: http://localhost:3000/allreferred?&referred_name=maria

request.path -> "/allreferred"

request.path -> "/allreferred"

request.base_url -> "http://localhost:3000"

request.base_url -> "http://localhost:3000"

request.fullpath -> "/allreferred?&referred_name=maria"

request.fullpath -> "/allreferred?&referred_name=maria"

request.original_url -> "http://localhost:3000/allreferred?&referred_name=maria"

request.original_url -> "http://localhost:3000/allreferred?&referred_name=maria"

回答by davegson

To retrieve the URLs:

要检索 URL:

# Current URL:
request.original_url

# Current relative URL:
request.request_uri

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

此外,您可以使用该current_page方法检查您是否在某条路线上。

current_page?(my_path)
# => true / false

For Rails 4 use:

对于 Rails 4 使用:

# Current URL:
request.original_url

# Current relative URL:
request.fullpath

回答by miguel savignano

You can use url_for

您可以使用 url_for

def current_path(params={})
  url_for(request.params.merge(params))
end

URL example http://localhost:3000/posts

网址示例 http://localhost:3000/posts

current_path -> /posts

current_path -> /posts

current_path(order: asc) -> /posts/?order=asc

current_path(order: asc) -> /posts/?order=asc