2010-02-20 63 views
0

我通过了auto_complete插件的railscast(这里是:http://railscasts.com/episodes/102-auto-complete-association),并且让它工作,即使我有一个稍微不同的设置。auto_complete插件,它可以提交一个ID而不是名称?

但这里的麻烦:在railscast中,例子是:belongs_to关联,并且我有一个has_many:through。我正在使用一个collection_select从一个下拉列表中选择要选择的成分列表,但如果我可以使用自动填充,它会更好。麻烦的是,来自collection_select的字段返回一个ingredient_id,这也是我需要的auto_complete行为。有没有办法使这个工作与auto_complete插件?

我不断寻找链接自动完成的Scriptaculous的演示,但现在不见了:(

前,用collection_select:现在

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

,与auto_complete半工作:

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

配方模型:

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 

回答

相关问题