I have a form for submission of brand, model, and style. Style
belongs_to model, and model belongs_to brand. The form is actually a
style submission form, but allows users to create new brands or models
as well, should they not exist.
What I want is for the autocomplete for ‘model’ to populate when an
object for ‘brand’ is selected in the first text field…
…and so on for submodel…
I have the following autocomplete field for brand
<%= form_for @style, :html => { :class => ‘form-horizontal’ } do |f| %>
<div class="control-group">
<%= f.label :brand_name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :brand_name, data: {autocomplete_source:
brands_path}, :class => ‘text_field’ %>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', styles_path, :class => 'btn' %>
</div>
<% end %>
and in styles.rb (model):
belongs_to :brand
def brand_name
brand.try(:name)
end
def brand_name=(name)
self.brand = Brand.find_or_create_by_name(name) if name.present?
end
and in styles.js.coffeescript:
jQuery ->
$(’#style_brand_name’).autocomplete
source: $(’#style_brand_name’).data(‘autocomplete-source’)