针对有些情况下下拉菜单过长导致选择不便,此处我将演示通过输入文字自动补全来选择
我这里要在product的view中自动补全thing
Gemfile中
gem 'rails3-jquery-autocomplete'
routes中
resources :products do collection do get :autocomplete_thing_name end end
thing.rb中(这里想显示name、material、size三个字段)
def name_with_material_and_size "#{name} - #{material} - #{size}" end
products_controller中
autocomplete :thing, :name, :full => true, :display_value => :name_with_material_and_size, :extra_data => [:name, :material, :size]
products.js.coffee
jQuery -> $("#autocomplete_thing_name").bind('railsAutocomplete.select', (e,data)-> $("#product_thing_id").val(data.item.id) )
product的view中
= f.hidden_field :thing_id = f.input :thing_id, :as => :autocomplete, :url => autocomplete_thing_name_products_path, :input_html => {:id => 'autocomplete_thing_name', :name => "thing_name"}