3

I'm using https://github.com/crowdint/rails3-jquery-autocomplete with a rails 3 web application and I would like to have an autocompleted field based not only on what has been typed on that field but also on the value of other fields in the same form.

Here is an example :

class Country < ActiveRecord::Base
    has_many :academic_titles_suggestions
end

class AcademicTitleSuggestion < ActiveRecord::Base
    belongs_to :country
end

class People < ActiveRecord::Base
    belongs_to :country
    # has field a field academic_title:string
end

Now when I display the form for a People, I wanto a drop down list for the country and a suggest box for the academic title based on the academic title suggestions of that country

Do you have any suggestions on how to do that ?

1

1 Answer 1

2

So you need to send extra fields and consider them for search.

You can use :fields option to send extra fields:

f.autocomplete_field :academic_title, fields: { country: "#country" }

Then you need to consider this extra field for search.

class AcademicTitleController < ActiveRecord::Base
  def autocomplete_academic_title
    country = params[:country]
    title = params[:title]

    titles = AcademicTitleSuggestion.joins(:country).where('countries.name = ? AND academic_title_suggestions.title LIKE ?', country, "%#{title}%").order(:title)
    render json: titles.map { |title| { id: title.id, label: title.title, value: title.title } }
  end
end

More info in the gem README

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.