0

我正在为我的 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"=>""}}
4

3 回答 3

4

似乎是 Rack 1.1 错误:http ://thewebfellas.com/blog/2010/7/15/rails-2-3-8-rack-1-1-and-the-curious-case-of-the-缺少引号

于 2010-12-15T12:38:21.493 回答
0

表单中的所有参数都将作为字符串到达​​控制器,rails 通过 activerecord 向数据库推断值,因此它知道您是否将“5”发送到数据库中的整数列,它应该将其更改为 5。但是对于搜索字符串,你需要做一些你自己的魔法。像这样:

irb(main):001:0> "ruby on rails".split(" ")
=> ["ruby", "on", "rails"]

它提供了一系列搜索词来搜索每个单独的词。

irb(main):006:0> terms
=> ["ruby", "on", "rails"]
irb(main):013:0> terms.each do |term|
irb(main):014:1* puts "this sentence on rails".match(term)
irb(main):015:1> end
nil
on
rails
=> ["ruby", "on", "rails"]
于 2010-11-23T11:42:31.317 回答
0

我无法在我的 Rails 2.3.5 中重现它。你确定不是浏览器在去掉引号吗?另外,如果您将 POST 用于搜索表单,是否会发生这种情况?

于 2010-11-23T11:54:30.007 回答