How to remove blank values params from query string
Asked Answered
K

4

9

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

http://localhost:3000/restaurants/search?utf8=%E2%9C%93&city=&cuisine=&number_of_people=&query=hello

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)

http://localhost:3000/restaurants/search?query=hello

How to do this?

One way can be using

CGI::parse("foo=bar&bar=foo&hello=hi")

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?

Krakau answered 19/6, 2012 at 13:47 Comment(5)
Why are blank values the problem?Dibucaine
@SergioTulentsev To make urls more clean..Krakau
@SergioTulentsev Its a search form. Once user submit the form. All comes up.Krakau
You can do smth like query.gsub(/\w+=&/, '').Explicate
I want to downvote Sergio's comments.Libeler
I
4

My solution was to disable blank inputs and selects:

$('form').submit (e) ->
  $(@).find('select,input').map( (i, e) -> e.disabled = !$(e).val() )

Regarding removing utf8 I found this. So I better keep sending it.

Doing all of this on server resulted in an additional request when using redirect_to, so I prefer to use client side code.

Irresistible answered 4/7, 2015 at 22:7 Comment(0)
R
3

In your controller method, call remove_empty_query_params

Then as a private method:

  def remove_empty_query_params
    # Rewrites /projects?q=&status=failing to /projects?status=failing
    require 'addressable/uri'
    original = request.original_url
    parsed = Addressable::URI.parse(original)
    return unless parsed.query_values.present?
    queries_with_values = parsed.query_values.reject { |_k, v| v.blank? }
    if queries_with_values.blank?
      parsed.omit!(:query)
    else parsed.query_values = queries_with_values
    end
    redirect_to parsed.to_s unless parsed.to_s == original
  end
Rupiah answered 5/5, 2016 at 3:9 Comment(0)
H
1

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" 
Hurryscurry answered 19/6, 2012 at 14:0 Comment(2)
not working for this: localhost:3000/restaurants/…Krakau
I did params.select!{|k, v| v.present?} which gets the same result, but I do not know how to make the response URL to be cleaned.Irresistible
B
0

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

url.chomp('&')
Bavardage answered 19/6, 2012 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.