Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Jquery - Checkbox change Ajax Call
    primarykey
    data
    text
    <p>I have to display checklist of things "to do" (in spanish).</p> <p><img src="https://i.stack.imgur.com/XdlYz.png" alt="Checklist example"></p> <p>The idea is once the checkbox associated to an item is checked, the text of the label gets a line through (text-decoration:line-through) and an attribute (named: done) from the database gets updated.</p> <p>To do this I have the following:</p> <p>In my <strong>view</strong>:</p> <pre><code>&lt;% @user_account.activities.each do |act| %&gt; &lt;div class="checklist_check"&gt; &lt;%= check_box_tag 'activity_status', act.id, act.done, :class =&gt; 'activity_status' %&gt; &lt;%= act.name %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>In the <strong>javascript</strong>:</p> <pre><code>$(function(){ $(".activity_status").live("change", function(act_id) { $.ajax({ url: "/controller/done", beforeSend: function() { alert("Hi") }, data: "id="+act_id, success: function() { alert('Bye') } }); }); }); </code></pre> <p>In my <strong>controller</strong>:</p> <pre><code>def done @activity = Activity.find params[:id] if @activity.done @activity.update_attributes :done_by_date, false else @activity.update_attributes :done_by_date, true end end </code></pre> <p>Also I set up the route for this action: get "controller/done"</p> <p><strong>The problem:</strong></p> <p>Once I click on a checkbox the function gets called and the alert("hi") gets executed but the success function does not and the "done" attribute never gets updated in the database.</p> <p>Any ideas of what am I doing wrong? How would you do it? </p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload