ruby on rails - How do you autocomplete from another model? -
i followed railscast on auto-complete associations stuck @ autocomplete half. not using prototype jquery instead , don't know how autocomplete half working. how done?
this customization compared railscast has:
products model:
def location_name location.business_name if location end def location_name=(business_name) self.location = location.find_by_business_name(business_name) unless business_name.blank? end
the above makes virtual attribute in defined location models :business_name
location_name
<%= f.text_field :location_name %>
of products form because product belongs location.
edit: using jquery autocomplete gem open alternatives.
my product :name
works charm;
<%= f.autocomplete_field :name, autocomplete_product_name_products_path %>
but if put :location_name
no good;
<%= f.autocomplete_field :location_name, autocomplete_product_location_name_products_path %>
it simple, had add attr_accessor
, finished code project:
<%= form_for(@business_address) |f| %> <%= f.error_messages %> <%= f.label :name, "address" %><br /> <%= f.autocomplete_field :name, autocomplete_business_address_name_business_addresses_path %> <%= f.label :business_name, "business" %> <%= f.autocomplete_field :business_name, autocomplete_business_name_business_addresses_path %> <%= f.submit %> <% end % class businessaddress < activerecord::base attr_accessible :name, :business_name belongs_to :business attr_accessor :business_name def business_name business.name if business end def business_name=(name) self.business = business.find_or_create_by_name(name) unless name.blank? end end class businessaddressescontroller autocomplete :business, :name, :full => true autocomplete :business_address, :name, :full => true end
routes.rb
resources :business_addresses :autocomplete_business_name, :on => :collection :autocomplete_business_address_name, :on => :collection end
make sure specify in application layout files needed or strange reason won't work (rails 3.0.9)
<%= javascript_include_tag "autocomplete-rails", "jquery-ui-1.8.16.custom.min" %>
Comments
Post a Comment