我正在使用form_for
资源:quote
。我想在第一个字段 (a collection_select
) 已更改时执行一项操作。我也在使用fields_for :bikes
哪个has_many 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 %>
quote.js.coffee(更新):
ready = ->
$('#quote_bikes_make').change ->
chosen = $(this).find(":make")
alert chosen.data("desc")
$(document).ready(ready)
$(document).on('page:load', ready)
我正在使用#quote_bikes_make
,因为我在呈现的 HTML 中看到了这一点:select id="quote_bikes_make"