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 helper来完成此操作,或者我需要以某种方式解析并重现先前请求中的查询字符串?
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屋!