如何从查询字符串中删除空值params

时间:2021-11-19 15:04:00

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

我有一个搜索表单,有很多选项,提交到有Get请求的路由。 URL是这样的:

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?

首先在用户之间重定向用户,并在操作之间检查哪些参数是空白并删除它们然后最终重定向他的实际搜索操作。但这听起来很蹩脚。我怎样才能以更好的方式做到这一点?

4 个解决方案

#1


2  

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.

关于删除utf8我发现了这个。所以我最好继续发送它。

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

在服务器上执行所有这些操作会在使用redirect_to时产生额外的请求,因此我更喜欢使用客户端代码。

#2


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" 

#3


1  

In your controller method, call remove_empty_query_params

在您的控制器方法中,调用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

#4


0  

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

我建议,如果只看普通的旧红宝石,一个简单的gsub就足够了:

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

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

注意:这可能会在最后留下一个&符号,可以用它修剪

url.chomp('&')

#1


2  

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.

关于删除utf8我发现了这个。所以我最好继续发送它。

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

在服务器上执行所有这些操作会在使用redirect_to时产生额外的请求,因此我更喜欢使用客户端代码。

#2


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" 

#3


1  

In your controller method, call remove_empty_query_params

在您的控制器方法中,调用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

#4


0  

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

我建议,如果只看普通的旧红宝石,一个简单的gsub就足够了:

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

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

注意:这可能会在最后留下一个&符号,可以用它修剪

url.chomp('&')