Rails:在link_to 中保留GET 查询字符串参数 [英] Rails: Preserving GET query string parameters in link_to

查看:22
本文介绍了Rails:在link_to 中保留GET 查询字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个典型的搜索工具,它返回一个结果列表,这些结果可以分页、排序、使用不同的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆