Ruby-on-rails 如何在rails中向当前URL添加参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6625293/
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
How to add parameters to current URL in rails
提问by Ved
I have a logic where I allow sorting on price and relevance. I am doing this by passing parameters to controller. My URL has a parameter - 'sort' which can have a value - 'price_lowest' or 'default'. The links looks like:
我有一个允许对价格和相关性进行排序的逻辑。我通过将参数传递给控制器来做到这一点。我的 URL 有一个参数 - 'sort',它可以有一个值 - 'price_lowest' 或 'default'。链接看起来像:
<a href="<%= request.fullpath + '&sort=price_lowest' %>">lowest prices</a> |
<a href="<%= request.fullpath + '&sort=default' %>">relevance</a>
problem with the above code is that it "adds" parameters and does not "replace" them. I want to replace the value of &sort= parameter without adding a new value. E.g. I don't want :
上面代码的问题在于它“添加”了参数而不是“替换”它们。我想替换 &sort= 参数的值而不添加新值。例如我不想:
../&sort=price_lowest&sort=price_lowest&sort=default
With the current logic - I am getting the above behaviour. Any suggestions ?
按照当前的逻辑 - 我得到了上述行为。有什么建议 ?
回答by lulalala
In order to preserve the params I did this:
为了保留参数,我这样做了:
<%= link_to 'Date', params.merge(sort: "end_date") %>
However the url will be ugly.
但是网址会很丑。
UPDATE
更新
For Rails 5 use:
对于 Rails 5 使用:
<%= link_to 'Date', request.params.merge(sort: "end_date") %>
回答by idlefingers
If you only need one cgi param and want to stay on the same page, this is very simple to achieve:
如果您只需要一个 cgi 参数并希望停留在同一页面上,那么实现起来非常简单:
<%= link_to "lowest prices", :sort => "price_lowest" %>
However, if you have more than one, you need some logic to keep old ones. It'd probably be best extracted to a helper, but essentially you could do something like this to keep the other params..
但是,如果您有多个,则需要一些逻辑来保留旧的。它可能最好提取到帮助程序,但基本上你可以做这样的事情来保留其他参数..
<%= link_to "lowest prices", :sort => "price_lowest", :other_param => params[:other] %>
Named routes will only really help you here if you need to go to another page.
如果您需要转到另一个页面,命名路线只会在这里真正帮助您。
回答by scarver2
If a path is not passed to the link_tomethod, the current paramsare assumed. In Rails 3.2, this is the most elegant method for adding or modifying parameters in a URL:
如果未将路径传递给该link_to方法,params则假定为当前路径。在 Rails 3.2 中,这是在 URL 中添加或修改参数的最优雅的方法:
<%= link_to 'lowest prices', params.merge(sort: 'end_date') %>
<%= link_to 'relevance', params.merge(sort: 'default') %>
paramsis a Ruby hash. Using merge will either add a key or replace the value of a key. If you pass nilas the value of a key, it will remove that key/value pair from the hash.
params是一个 Ruby 哈希。使用合并将添加一个键或替换一个键的值。如果您nil作为键的值传递,它将从哈希中删除该键/值对。
<%= link_to 'relevance', params.merge(sort: nil) %>
Cite:
引用:
回答by Alex Freshmann
My working solution on Rails 3.1of course, it's hardcode, and has to be refactored.
我在 Rails 3.1 上的工作解决方案当然是硬编码,必须重构。
item model
物品模型
def self.get(field,value)
where(field=>value)
end
items controller
物品控制器
@items=Item.all
if params[:enabled]
@[email protected](:enabled, params[:enabled])
end
if params[:section]
@[email protected](:section_id, params[:section])
end
items helper
物品助手
def filter_link(text, filters={}, html_options={})
trigger=0
params_to_keep = [:section, :enabled]
params_to_keep.each do |param|
if filters[param].to_s==params[param] && filters[param].to_s!="clear" || filters[param].to_s=="clear"&¶ms[param].nil?
trigger=1
end
if filters[param]=="clear"
filters.delete(param)
else
filters[param]=params[param] if filters[param].nil?
end
end
html_options[:class]= 'current' if trigger==1
link_to text, filters, html_options
end
items index.html.erb
项目 index.html.erb
<%= filter_link 'All sections',{:section=>"clear"} %>
<% @sections.each do |section| %>
<%= filter_link section.title, {:section => section} %>
<% end %>
<%= filter_link "All items", {:enabled=>"clear"} %>
<%= filter_link "In stock", :enabled=>true %>
<%= filter_link "Not in stock", :enabled=>false %>
回答by Dave A-R
It's not quite the answer to the question that you're asking, but have you thought about using the Sorted gemto handle your sorting logic and view links?
这不是您所问问题的答案,但是您是否考虑过使用Sorted gem来处理您的排序逻辑和查看链接?

