Rails:在 link_to 中保留 GET 查询字符串参数 [英] Rails: Preserving GET query string parameters in link_to
问题描述
我的应用程序中有一个典型的搜索工具,它返回一个可以分页、排序、使用不同的 records_per_page 值查看的结果列表等.这些选项中的每一个都由查询字符串中的参数控制.一个简化的例子:
I have a typical search facility in my app which returns a list of results that can be paginated, sorted, viewed with a different records_per_page value, etc. Each of these options is controlled by parameters in the query string. A simplified example:
/search?q=test&page=2
现在假设我需要显示一组将 records_per_page 值设置为 10、20、30 的链接.每个链接必须包含现有的查询参数,这些参数可以是一个很长的集合,再加上一个新的 per_page 参数.
Now say I need to display a set of links that set records_per_page value to 10, 20, 30. Each link must include the existing query parameters, which can be a very long set, plus a new per_page parameter.
/search?q=test&page=2& ... &per_page=10
/search?q=test&page=2& ... &per_page=20
/search?q=test&page=2& ... &per_page=30
是否有一种简单的方法可以只使用 link_to 帮助程序,或者我需要以某种方式解析和重现来自先前请求的查询字符串?
Is there an easy way to do it with just link_to helper or I need to parse and reproduce the query string from previous request somehow?
推荐答案
link_to 'Link', request.query_parameters.merge({:per_page => 20})
这篇关于Rails:在 link_to 中保留 GET 查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!