Take the 2-minute tour ×

I have a search form, with lot of options, Submitted to a route with Get request. URL is something like this:


with lot more params. I want to make it cleaner something like remove all the params which are blank.

something like this: (Basically removing all the params which are blank)


How to do this?

One way can be using


Gives you

{"foo"=>["bar"], "hello"=>["hi"], "bar"=>["foo"]}

First redirect user on a in between action and in that in between action check which params are blank and remove them and then finally redirecting him on the actual action of search. But this sounds very lame thing. How can i do this in a better way?

share|improve this question
Why are blank values the problem? –  Sergio Tulentsev Jun 19 '12 at 13:51
@SergioTulentsev To make urls more clean.. –  Mohit Jain Jun 19 '12 at 13:51
Then don't send them in the first place. –  Sergio Tulentsev Jun 19 '12 at 13:52
@SergioTulentsev Its a search form. Once user submit the form. All comes up. –  Mohit Jain Jun 19 '12 at 13:53
I want to downvote Sergio's comments. –  Arcolye Nov 30 '12 at 6:28

2 Answers 2

Just with plain ol' ruby...

require 'uri'
a = "http://localhost:8080/path/search?foo=&bar=&baz=2&bat=thing"
u = URI.parse(a)
params = u.query.split("&").select {|param| param =~ /=./}.join("&")
# Returns "baz=2&bat=thing" 
share|improve this answer
not working for this: localhost:3000/restaurants/… –  Mohit Jain Jun 19 '12 at 14:11
it works fine here. –  Lars Haugseth Jun 19 '12 at 14:28

I would suggest, if just looking at plain old ruby, a simple gsub might be enough:

url.gsub(/[^\?&=]+=(?:&|$)/, '')

Note: this may leave an ampersand at the end, which can be trimmed with

share|improve this answer

Your Answer


