views:

14

answers:

0

I went through the railscast for the auto_complete plugin (here: http://railscasts.com/episodes/102-auto-complete-association), and got it working, even though I have a slightly different setup.

But here's the trouble: in the railscast, the example is a :belong_to association, and I have a has_many :through. I was using a collection_select to get a list of ingredients to choose from a drop down, but it will be a whole lot better if I can use the autocomplete. Trouble is, the field from the collection_select returns an ingredient_id, which is the behavior I need for the auto_complete as well. Is there a way to make this work with the auto_complete plugin?

I keep finding links to a scriptaculous demo of autocomplete, but it's gone now :(

Before, with collection_select:

<%= f.collection_select :ingredient_id, Ingredient.all, :id, :name, :prompt => "Select an Ingredient"%>

Now, with auto_complete half-working:

<%= text_field_with_auto_complete :ingredient, :ingredient_name, {:size => 15}, {:url => formatted_ingredients_path(:js), :method => :get, :with => "'search=' + element.value"} %>

recipe model:

has_many :ingredient_amounts
    has_many :ingredients, :through => :ingredient_amounts

def ingredient_name

end

def ingredient_name=(name)
  self.ingredient = Ingredient.find_by_name(name) unless name.blank?
end