0

I am using form_for a :quote resource. I would like to perform an action when the first field (a collection_select) has been changed. I am also using fields_for :bikes which has_many quotes

Here is my code right now:

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 using #quote_bikes_make because I see this in the rendered HTML: select id="quote_bikes_make"

IkegawaTaro
  • 3,563
  • 5
  • 21
  • 26

1 Answers1

1

@mu is too short was right in that I needed to put the coffeescript in a ready hook (see updated code above). However, this alone didn't solve the problem. I installed the jquery-turbolinks gem which did the trick.

IkegawaTaro
  • 3,563
  • 5
  • 21
  • 26