如何从查询字符串中删除空白值参数 [英] How to remove blank values params from query string

查看:142
本文介绍了如何从查询字符串中删除空白值参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多选项的搜索表单,已通过获取"请求提交给路线.网址是这样的:

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

如何执行此操作?

可以使用一种方法

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

给你

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

首先在两次操作之间以及一次操作之间将用户重定向,检查哪些参数为空并删除它们,然后最终根据搜索的实际操作将其重定向.但这听起来很la脚.我怎样才能更好地做到这一点?

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?

推荐答案

我的解决方案是禁用空白输入并选择:

My solution was to disable blank inputs and selects:

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

关于删除utf8,我发现了.所以我最好继续发送它.

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

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

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

这篇关于如何从查询字符串中删除空白值参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆