在Rails 4中将参数添加到当前URL的简单方法?

时间:2022-08-30 10:24:22

On a particular page in my website, there are a variety of potential URL parameters:

在我的网站的特定页面上,有各种潜在的URL参数:

http://www.example.com/my_webpage?at=2014-01-01&page=5

Now I want a simple way to add a parameter to that like this:

现在我想要一个简单的方法来添加一个参数,如下所示:

http://www.example.com/my_webpage?at=2014-01-01&page=5&records=100

So I tried the following HTML with embedded Ruby:

所以我尝试使用嵌入式Ruby的以下HTML:

<form action="<%= request.original_url %>" method="get"># of records <input type="text" name="records"/></form>

The problem is the resulting page that opens is:

问题是打开的结果页面是:

http://www.example.com/my_webpage?records=100

Essentially, the old parameters get wiped away. What's an easy way to retain them? I could loop through the params hash and add hidden_tags (I'd have to selectively exclude params not part of the request params), but I would expect with such a common use case scenario there's a better easier way.

基本上,旧的参数被抹去了。保留它们的简单方法是什么?我可以遍历params哈希并添加hidden_​​tags(我必须有选择地排除params而不是请求参数的一部分),但我希望通过这种常见的用例场景,有一种更简单的方法。

1 个解决方案

#1


-1  

While there isn't an easy Rails way of doing this automatically, Rails does provide access to the request parameters through the request.query_parameters hash. So I simply needed to add this in the form:

虽然没有一种简单的Rails方法可以自动执行此操作,但Rails确实通过request.query_parameters哈希提供对请求参数的访问。所以我只需要在表单中添加:

<% request.query_parameters.each do |key, val|%>
  <input type="hidden" name="<%= key %>" value="<%= val %>"/>
<% end %>

#1


-1  

While there isn't an easy Rails way of doing this automatically, Rails does provide access to the request parameters through the request.query_parameters hash. So I simply needed to add this in the form:

虽然没有一种简单的Rails方法可以自动执行此操作,但Rails确实通过request.query_parameters哈希提供对请求参数的访问。所以我只需要在表单中添加:

<% request.query_parameters.each do |key, val|%>
  <input type="hidden" name="<%= key %>" value="<%= val %>"/>
<% end %>