Ruby-on-rails link_to 当前页面加上/与参数合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5535908/
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-03 00:51:02  来源:igfitidea点击:

link_to the current page plus/merged with a param

ruby-on-railsruby-on-rails-3

提问by Steven

A bit of a softball question, but my Google Fu escapes me at the moment.

有点垒球问题,但我的 Google Fu 此刻让我逃过了一劫。

How do I link to the current page but then specify params to be merged into the query string?

如何链接到当前页面,然后指定要合并到查询字符串中的参数?

For instance, if the current page is /resources, I want to be able to specify something like current_page_plus(:param => 'attribute')that evaluates out to /resources?param=attribute.

例如,如果当前页面是/resources,我希望能够指定类似current_page_plus(:param => 'attribute')评估为 的内容/resources?param=attribute

This would hopefully also merge with existing attributes, so if the current page was /resources?a=b&c=d, then current_page_plus(:c => 'e')would evaluate to /resources?a=b&c=e.

这也有望与现有属性合并,因此如果当前页面是/resources?a=b&c=dcurrent_page_plus(:c => 'e')则将评估为/resources?a=b&c=e

回答by Simone Carletti

You can use url_forpassing the params.

您可以使用url_for传递参数。

url_for(params.merge(:c => "e"))

If you use an helper like link_tothat internally uses url_foryou can skip the url_forand pass the has directly.

如果您使用link_to内部使用的助手,则url_for可以跳过url_for并直接传递 has。

link_to "Page", params.merge(:c => "e")