Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>uri = URI.parse(request.url) new_query = uri.query.split("&amp;").reject{|pair| k,v = pair.split("=") k =~ /^foo\[?/ # reject params named "foo" or "foo[anything" }.join("&amp;") # if the new querystring is empty, assign nil instead of "" so we don't # end up with a solitary question mark at the end of the url. uri.query = (new_query.empty? ? nil : new_query) redirect_to uri.to_s </code></pre> <p>I came to the above solution after running into some non-obvious problems with trying to do things the "Rails way," if there is such a way, to solve this issue. For instance:</p> <ul> <li>Rails parameter parsing will automagically instantiate arrays and hashes for query params that use the special square bracket syntax: <code>?x[a]=1&amp;y[]=2</code> becomes <code>{"x"=&gt;{"a"=&gt;"1"}, "y"=&gt;["2"]}</code> and so on, so code that uses the provided querystring and param routines are tricky to deal with in this regard. And filtering params by raw string value can be difficult as a result. Even the non-rails/rack CGI module will give you arrays instead of strings for the param values.</li> <li>The rails <code>params</code> hash includes both GET, POST, and routing parameters, such as <code>:action</code> and <code>:controller</code> so you couldn't just re-use that to reconstruct a querystring anyway.</li> <li>As such, the URL helpers like url_for are not much help.</li> </ul> <p>Given these issues I found I was not able to use provided framework functionality to get a pure and unmolested querystring, reproduced as-is from the current request, that simply and only removes one parameter. The code above seems to work well, however.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload