如何在rails中获取当前路由

我正在构建一些rails示例应用程序,其中我有两个模型User和Projects。 两者之间的关联是用户has_many项目。 现在的问题是我愿意为用户的项目提供一些下拉菜单,如果有人点击那个项目,那么将项目显示页面。 但是如果用户在一个项目的编辑页面并从下拉列表中选择其他项目我想让他到显示页面的任何想法到达新选项目的编辑页面相同的情况。 我正在寻找的解决方案是: – 我们可以使用params [:controller]找到当前控制器,使用params [:action]查找当前操作如何找到当前路径。 提前完成

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

要检索url:

# Current URL: request.original_url # Current relative URL: request.request_uri 

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

 current_page?(my_path) # => true / false 

对于Rails 4使用:

 # Current URL: request.original_url # Current relative URL: request.fullpath 

对于Rails 4

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

request.path -> "/allreferred"

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

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

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

你可以使用url_for

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

URL示例http://localhost:3000/posts

current_path -> /posts

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