risuiowa / rails-jquery-autocomplete

An easy and unobtrusive way to use jQuery's autocomplete with Rails 3 and later.
MIT License
212 stars 80 forks source link

Is it possible to have more than one autocomplete? #109

Closed acesuares closed 3 years ago

acesuares commented 3 years ago

I have a words_controller with:

autocomplete :word, :name, full: false, limit: 12

I added a second autocomplete:

autocomplete :words_with_pictures, :name, full: false, limit: 12

I also added a route for the second autocomplete:

  resources :words do
    get :"autocomplete_words_with_pictures_name", :on => :collection
  end

I added a method in the controller:

  def autocomplete_words_with_pictures_name
    term = params[:term]
    words = Word.has_pictures.where('words.name LIKE ?', "%#{term}%").order(:name).all
    render :json => words.map { |word| {:id => word.id, :label => word.name, :value => word.name} }
  end

The following works now (it generates JSON):

/words/autocomplete_words_with_pictures_name?term=ala

But this doesn't work:

/words/autocomplete_word_name?term=ala

If I remove the route for words_with_pictures

  # resources :words do
  #   get :"autocomplete_words_with_pictures_name", :on => :collection
  # end

then it still doesn't work, but if I reverse the order of the autocomplete statements in the controller:

  autocomplete :words_with_pictures, :name, full: false, limit: 12
  autocomplete :word, :name, full: false, limit: 12

then this works:

/words/autocomplete_word_name?term=ala

Obviously, the other one doesn't work since there is no route for it anymore.

My questions:

  1. Can have two autocompletes in the same controller?
  2. If not, how can I define the scope (or any other behaviour) in the view? For instance, something like
        <%= autocomplete_field_tag :add_word, '', send("autocomplete_word_name_words_path"), :placeholder => 'buska palabra...', :id => 'input_search', :scope => :has_pictures %>
acesuares commented 3 years ago

I now tried something different. On the words_controller, I have

autocomplete :word, :name, full: false, limit: 12

On the memory_games_controller, I have

autocomplete :word, :name, full: false, limit: 12, scopes: [:has_pictures]

In routes.rb, I added

resources :memory_games do
  get :"autocomplete_word_name", :on => :collection
end

rake:routes give both routes:

autocomplete_word_name_words GET      /words/autocomplete_word_name(.:format)                     words#autocomplete_word_name
autocomplete_word_name_memory_games GET      /memory_games/autocomplete_word_name(.:format)              memory_games#autocomplete_word_name

In the view, I can use:

<%= autocomplete_field_tag :add_word, '', send("autocomplete_word_name_words_path"), :placeholder => 'buska palabra...', :id => 'input_search' %>

and this works, but of course it is showing all words, not just the one with pictures. When I change it to:

<%= autocomplete_field_tag :add_word, '', send("autocomplete_word_name_memory_games_path"), :placeholder => 'buska palabra...', :id => 'input_search' %>

I get the error:

"GET /memory_games/autocomplete_word_name?term=ala HTTP/1.1" 404 15077

I'm lost.

acesuares commented 3 years ago

Well, I moved the route up, above the one that declares /words/autocomplete_word_name, and now it works

autocomplete_word_name_memory_games GET      /memory_games/autocomplete_word_name(.:format)              memory_games#autocomplete_word_name
autocomplete_word_name_words GET      /words/autocomplete_word_name(.:format)                     words#autocomplete_word_name

I am still lost, but hey! it works.