Rails How to Use JQuery to Display a Record Above a Form When an Event is Triggered?

I am using Rails 6.1.4 and ruby 2.6.7

I have an app that tracks weight lifting exercises. On the new record form (view) I have a select box to pick an exercise to perform. What I would like to do is when the onChange event for the drop down is triggered, I would like to fetch the last record created for that exercise and display it as text above the form. I do not want the record to show in the form elements, just as text. I or users can use the last record to gauge their progress.

I’ve done this sort of programming many times in PHP using JQuery and ajax calls, but have never done it with rails. I do not know how an ajax call to a separate file (helper?) can send a value to the controller (new action?) and be seen on the new form without making a separate request to the form page.

My view:

apps/views/fitness/weights/new (_form)

<div class="grid-x">                                                                      
    <div class="cell medium-2">Exercise/Routine:</div>
    <div class="cell medium-2"><%= f.select(:exercise, options_for_select(@exercises)) %></div>

The above becomes:

<select name="fitness_weight[exercise]" id="fitness_weight_exercise">

Are there any tutorials on this? I have not found any in my Googling.
Any pointers would be appreciated.


Source: JavaSript – Stack Overflow

November 28, 2021
Category : News
Tags: ajax | javascript | jquery | ruby-on-rails

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.