0

I'm using the twitter-typeahead-rails gem to implement instant search on my app (search results populate a dropdown menu as you type). The problem I'm getting is that searching for the "." character seems to give bizarre results, but apparently only when the results are translated into json.

This is my typeahead method*, utilizing instant search and therefore json:

def typeahead
  q = params[:query]
  render json: Singer.where('name ilike ?', "%#{q}%").first(5)
end

And this is a different method, which redirects to a page with search results, and doesn't use json:

def search
  q = params[:query]
  results = Singer.where('name ilike ?', "%#{q}%").first(5)
end

On the second method (called "search"), searching q = "t.i." behaves as expected, and gives me a results page with a Singer with name: "T.I.".

On the first method ("typeahead"), however, searching for q = "t.i." returns no results. When I search for q = "t.i" I get a Singer with name: "Taylor Swift" as a result, and I can't fathom why. q = "t." also returns no results. Strings with two dots and three letters, like "t.i.a" also return no results. However, any searches without dots work normally.

As far as I can tell, the "." is for some reason being interpreted differently in my first method, but again, I can't imagine why. I've tried searching for what, if anything, "." means in SQL, but can't find anything. The only difference between the two methods seems to be that the first one is rendered in json. But why would that make any difference?


*I didn't include the code for searching for or rendering the results, because it's very long and I figured the only code that's relevant is the code that retrieves the records from the database, because that is what is failing.

#Gemfile
gem 'rails', '4.0.10'
gem 'pg'
gem 'twitter-typeahead-rails', '0.10.5'
Joe Morano
  • 1,715
  • 10
  • 50
  • 114
  • may be, the problem in routes.rb http://stackoverflow.com/a/5369702/1297435 – rails_id Feb 07 '17 at 09:01
  • Agreed with @rails_id. Could you include the path definitions from your `config/routes.rb` file related to the `typeahead` and `search` actions? You're probably missing some constraints to allow dots in the `query` parameter. – hattila91 Feb 07 '17 at 10:11
  • @rails_id Yep, that was the problem. Thanks. – Joe Morano Feb 07 '17 at 21:36

0 Answers0