After selecting a value in a form created with Rails 4.0, CoffeeScript fails to trigger the alert message

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"

Answer №1

After taking @mu is too short's advice and placing the coffeescript in a ready hook (refer to updated code above), I encountered further issues that needed addressing. Thankfully, incorporating the jquery-turbolinks-addon extension resolved the issue at hand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Allow users to edit the textarea only to input values, not from

I am trying to achieve a specific functionality with two input fields and one textarea. Whenever I type something in the input fields, their values are automatically populated in the textarea using .val(). Currently, this feature is working as intended, b ...

Ways to display or conceal a component based on the current component being used

I am a newcomer in Angular, and I believe finding the solution to my problem will be a great learning experience. In my default component, the MainComponent is loaded in my <router-outlet>. At this stage, both the menu and the footer are displayed. H ...

Comparison of Uint8Array and Uint8ClampedArray

Can you explain the distinction between Uint8Array and Uint8ClampedArray within JavaScript? I've heard that Uint8ClampedArray is specifically utilized for pixel manipulations on canvas. Could you elaborate on why this array type is recommended for suc ...

Extract information from a JSON string using a foreach loop to handle multiple occurrences of the same data type

I am working with a JSON object that I need to parse in order to retrieve specific data. The structure is as follows: "id": "5019b4b40cd8a056446b8eb4", "checkItemStates": [ { "idCheckItem": "xxxxxxxx", "state": "complete" }, { "idCheckItem ...

Create a JavaScript variable every few seconds and generate a JSON array of the variable whenever it is updated

My variable, which consists of random numbers generated by mathrandom every second, such as "14323121", needs to be saved to an array for the latest 10 updates. function EveryOneSec() { var numbers = Math.random(); // I want to create an array from th ...

Using regular expressions in Sublime Text 2 to replace text in JSON files can greatly improve your workflow

After using an online web tool to convert an Excel file to JSON, I now require assistance in replacing certain values within the JSON. Currently, I am using Sublime Text 2. {"Time (GMT-04:00)":"2010-07-06 08:30:00","Skin temp - average":"34,2043","Step Co ...

The paths required are not correct following the transpilation of a TypeScript file using Babel

Issue Every time I use nodemon with npm run start, I encounter the error message "Error: Cannot find module 'Test'". Similarly, when I build files using npm run build and try to run ./dist/index.js, I face the same issue. It seems that the requ ...

Javascript time intervals run rapidly during tab changes

I am facing an issue where my neck is constrained at regular time intervals. I have a function that helps me calculate time one second at a time: Header.prototype= { time_changed : function(time){ var that = this; var clock_handle; ...

A perpetual JavaScript countdown that constantly loops with a delayed response

I'm currently working on an online auction bidding project that involves users placing bids before time runs out. I found a solution on this link. My goal is to run the timer loop continuously. When the timer hits 0, I want the bid button to be disab ...

Increase the date by one day excluding weekends (Saturday and Sunday)

My current code is designed to add +1 day to the current date. var date = '30 Apr 2010'; var actual_date = new Date(date); var final_date = new Date(actual_date.getFullYear(), actual_date.getMonth(), actual_date.getDate()+1); Now, I am looking ...

What are some ways we can enhance shadows in Three.js?

Exploring three.js over on 2toria.com/pool Struggling with enhancing the quality of my shadows. Currently facing this issue:- The shadows appear a bit pixelated. Is there a way to achieve smoother shadows like this:- Experimented with different settings ...

Multiple buttons are linked to a function, causing the sharing of internal variables

I have a situation where I need to bind ajax calls to multiple buttons, each button being unique based on attached data attributes. The issue arises when the internal variables of the function are shared among different buttons. This leads to problems if ...

Simple integration of JSP, JSON, and AJAX

I need help testing a simple login functionality using AJAX. Here's the code snippet: <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login Test Page</title> <script src="../js/j ...

React-based video annotation/overlay features in the style of Youtube tutorials

Our team is in need of a video annotation system similar to YouTube's. We require the ability to add text/images over videos at specific times. I tried looking for React components or vanilla JS libraries for guidance, but couldn't find any. If ...

Chapter 5 of Eloquent JavaScript's 3rd Edition presents Exercise 3

I'm struggling with an exercise in the latest edition of a book, specifically in Chapter 5 which covers Higher-Order Functions. The prompt for the exercise is as follows: "Similar to the some method, arrays also contain an every method. This method r ...

Determining if a mouse is currently hovering over an element using Javascript

I am working on a script that logs a message to the console if the user keeps their mouse over an element for more than 2 seconds. Here is the code snippet I have so far: var $els = document.querySelectorAll('#myDiv'); for(i = 0; i < $els ...

Automate the process of uploading images using Selenium, without the need for AutoIt

My company's website features a unique image upload button that differs from the traditional input type file. I'm looking for an automated way to upload images using this button without relying on tools like AutoIt to interact with the file explo ...

Protect your Vercel Next.js serverless function with secure authentication mechanisms

I'm looking to restrict access to my GraphQL API to authenticated users only. I've been using Apollo GraphQL Studio to test my API, and I've successfully set the auth token in the header. However, I'm unsure of how to retrieve and use t ...

Implementing row updates using contenteditable feature in Vue.js

I am currently exploring how to detect and update the changes made in a 'contenteditable' element within a specific row. <tbody> <!-- Iterate through the list and retrieve each data --> <tr v-for="item in filteredList& ...

Using Typescript to Declare Function and React Component Types

Currently challenging myself to delve into Typescript (ugh). Usually, I can deduce the appropriate type by analyzing the return values. Nonetheless, in this particular scenario, that method is proving ineffective. type SomeReactAProps = { Type1: ReactEle ...