When working with a :quote
resource, I want to trigger a function when the value of the first field (a collection_select
) is changed. Additionally, my code includes fields_for :bikes
, which represents a relationship where bikes has_many quotes
.
Below is the current version of my code:
quotes/new.html:
<% provide(:title, 'New Quote') %>
<h1>Get a Quote</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for @quote do |f| %>
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :bikes do |builder| %>
<%= f.label :make, 'Make' %>
<%= f.collection_select :make, Bike.unique_by_make, :id , :make %>
<%= f.label :model, 'Model' %>
<%= f.text_field :model %>
<%= f.label :year_manufactured, 'Year' %>
<%= f.date_field :year_manufactured %>
<% end %>
<%= f.submit "Show Quote", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
<%= link_to 'Back', quotes_path %>
quotes.js.coffee (updated):
ready = ->
$('#quote_bikes_make').change ->
chosen = $(this).find(":make")
alert chosen.data("desc")
$(document).ready(ready)
$(document).on('page:load', ready)
I am specifically referencing #quote_bikes_make
because in the HTML, it appears as select id="quote_bikes_make"