0

Hi I have been searching for a while now I do not understand why my autocomplete does not work using jquery-ui and rails.

Lesson.rb:

class Lesson < ActiveRecord::Base 
  def self.search(term)
    where('LOWER(title) LIKE :term OR LOWER(artist) LIKE :term', term: "%#{term.downcase}%")
  end
end

LessonsController:

def search
  respond_to do |format|
    format.json { @lessons = Lesson.search(params[:term]) }
  end
  @lessons.each do |lesson|
    puts lesson.title
  end
end

Lessons.coffee:

$ ->
  $('#search').autocomplete
    source: 'lessons/search'
    response: (event, ui) ->
    # ui.content is the array that's about to be sent to the response callback.
      if ui.content.length == 0
        console.log('No results found')
      else
        console.log(ui.content.length + "lessons were found")

search.json.jbuilder:

json.array!(@lessons) do |lesson|
  json.title        lesson.title
  json.artist       lesson.artist
end

home.slim:

input#search type="search" placeholder="Recherchez un cours"

Logs:

Started GET "/lessons/search?term=s" for 127.0.0.1 at 2016-02-12 14:51:24 +0100
Processing by LessonsController#search as JSON
  Parameters: {"term"=>"s"}
  Lesson Load (0.3ms)  SELECT "lessons".* FROM "lessons" WHERE (LOWER(title) LIKE '%s%' OR LOWER(artist) LIKE '%s%')
Sweet Child o' mine
hotel california
  Rendered lessons/search.json.jbuilder within layouts/application (3.6ms)
  Rendered layouts/partials/_navigation.slim (5.5ms)
  Rendered layouts/partials/_footer.slim (6.1ms)
Completed 200 OK in 146ms (Views: 131.8ms | ActiveRecord: 0.5ms)

console.log(ui.content.length) also displays no results.

2
  • You might want to give a second look to your LessonsController's search method.
    – x6iae
    Commented Feb 12, 2016 at 14:18
  • Actually it does work if I render json but I get objects while I want to use Jbuilder to get only the titles.
    – Snake
    Commented Feb 12, 2016 at 17:12

0