0
class SearchController < ApplicationController
  def autocomplete
    @location=Location.find_by_sql("select * from locations where name like ?;",['%'+params[:term]+'%'])
    render  json: @location
  end
end

I think query like this is not working as expected: select * from locations where name like '%XXXX%';

Neither can I wrote code like this: @location=Location.find_by_sql("select * from locations where name like '?';", the function will fail to recognize the params

So how could I implement such keywords filtering queries?

Yitong Zhou
  • 1,155
  • 4
  • 22
  • 42

1 Answers1

0

This should work:

@location = Location.where("name LIKE ?", "%#{params[:term]}%")
Dogbert
  • 212,659
  • 41
  • 396
  • 397