1

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

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:

<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.

at.
  • 50,922
  • 104
  • 292
  • 461
  • Apparently, you will have to use hidden_tags. There is not better way. [See](http://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear) – Nitish Parkar Oct 03 '14 at 06:23
  • Is there an easy way to iterate through the request params in Rails? I mean the `params` hash contains the request params as well as parameters in the URL. – at. Oct 03 '14 at 06:44

1 Answers1

-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:

<% request.query_parameters.each do |key, val|%>
  <input type="hidden" name="<%= key %>" value="<%= val %>"/>
<% end %>
at.
  • 50,922
  • 104
  • 292
  • 461