Problem with Rails stripping quotes from input form
-
27-09-2019 - |
Question
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"=>""}}
Solution
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
OTHER TIPS
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"]
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?