Rails 从输入表单中删除引号时出现问题
我正在为我的 Rails(2.3.9,但我已经检查过这个问题也存在于 3.0.3)应用程序中编写一个搜索表单。 问题在于 Rails 正在从用户输入中删除引号。 我想让用户可以写:
- “Ruby on Rails”:这将搜索整个字符串的全文
- ruby on Rails:这将搜索包含所有这三个单词的文章
但是在我的控制器中,对于这两种情况我'我只得到一个字符串:
Processing NewsController#index (for 127.0.0.1 at 2010-11-23 10:23:15) [GET]
Parameters: {"action"=>"index", "controller"=>"news", "search"=>{"category"=>"", "news_agency"=>"", "fullsearch"=>"ruby on rails", "order"=>""}}
是否有任何选项可以跳过这一过程,去掉引号?
备注: 当用户在搜索字符串两侧添加空格时,例如: ' "ruby on Rails" ' 字符串将被正确发送:
Processing NewsController#index (for 127.0.0.1 at 2010-11-23 10:23:15) [GET]
Parameters: {"action"=>"index", "controller"=>"news", "search"=>{"category"=>"", "news_agency"=>"", "fullsearch"=>" \"ruby on rails\" ", "order"=>""}}
I'm writing a search form for my Rails (2.3.9 but I've checked that this problem exists also on 3.0.3) application.
The problem is that Rails is stripping quotation marks from the users input.
I would like to give the users possibility to write:
- "ruby on rails" : and this would search full text for the whole string
- ruby on rails : this would search for articles with all those three words
But in my controller for both cases I'm getting only one string:
Processing NewsController#index (for 127.0.0.1 at 2010-11-23 10:23:15) [GET]
Parameters: {"action"=>"index", "controller"=>"news", "search"=>{"category"=>"", "news_agency"=>"", "fullsearch"=>"ruby on rails", "order"=>""}}
Is there any option to skip this stripping the quotation marks?
Remark:
When user adds spaces to the both sides for the search string for example:
' "ruby on rails" ' the string will get correctly sent:
Processing NewsController#index (for 127.0.0.1 at 2010-11-23 10:23:15) [GET]
Parameters: {"action"=>"index", "controller"=>"news", "search"=>{"category"=>"", "news_agency"=>"", "fullsearch"=>" \"ruby on rails\" ", "order"=>""}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎是 Rack 1.1 的错误: http://thewebfellas.com/blog/2010/7/15/rails-2-3-8-rack-1-1-and-the-curious -缺少引号的情况
Seems to be a Rack 1.1 bug: http://thewebfellas.com/blog/2010/7/15/rails-2-3-8-rack-1-1-and-the-curious-case-of-the-missing-quotes
表单中的所有参数都将作为字符串到达控制器,rails 通过 activerecord 将值推断到数据库,因此它知道是否将“5”发送到数据库中的整数列,它应该将其更改为 5。但对于搜索字符串,你需要施展一些你自己的魔法。像这样:
它提供了一系列搜索词来搜索每个单独的词。
all params from a form will arrive to the controller as strings, rails infers values to the database via activerecord, so it knows if you send "5" to an integer column in the db, that it should change it to 5. but for search strings, you need to do some of your own magic. like so:
which provides an array of search terms to search against each individual term.
我无法在 Rails 2.3.5 中重现它。您确定不是浏览器正在剥离引号吗?另外,如果您使用 POST 作为搜索表单,会发生这种情况吗?
I can't reproduce it in my Rails 2.3.5. Are you sure it's not browser that is stripping the quotation marks? Also, does it happen if you use POST for the search form?