Rails 从输入表单中删除引号时出现问题

发布于 2024-10-03 14:03:38 字数 889 浏览 0 评论 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"=>""}}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天冷不及心凉 2024-10-10 14:03:38

表单中的所有参数都将作为字符串到达​​控制器,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"]

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:

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

which provides an array of search terms to search against each individual term.

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"]
○愚か者の日 2024-10-10 14:03:38

我无法在 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文