我正在构建一些rails示例应用程序,其中我有两个模型User和Projects。两者之间的关联是用户has_many项目。现在的问题是我愿意为用户的项目提供一些下拉菜单,如果有人点击那个项目,那么将项目显示页面。但是如果用户在一个项目的编辑页面并从下拉列表中选择其他项目我想让他到达新选择项目的编辑页面同一个案例对于显示页面的任何想法。我正在寻找的解决方案是: - 我们可以使用params [:controller]找到当前控制器,使用params [:action]查找当前操作如何找到当前路由。 Thanx提前
如果有人想看看我的代码是什么: - 这里是github的链接: - 'https://github.com/peeyushsingla/session_test.git/这里我使用的是简单的链接,但实际上我会提供一些可以显示的单个下拉菜单,适用于所有编辑,显示,新的每个操作
答案 0 :(得分:24)
检索网址:
# 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
答案 1 :(得分:24)
对于Rails 4
网址示例: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"
答案 2 :(得分:3)
您可以使用url_for
def current_path(params={})
url_for(request.params.merge(params))
end
网址示例http://localhost:3000/posts
current_path -> /posts
current_path(order: asc) -> /posts/?order=asc