Aaron
Aaron

Reputation: 4480

Call update method when a checkbox is checked

I am trying to figure out how to call the update method when the user checks on the check box. I want to update the database by deleting the value for the check box that is stored in the database. Here is my code

<% @items.each do |item|%>
    <%if item.email == @user.email%>
        <%= form_for @item do |f| %>
            <%= f.check_box :to_do, url: update,:value => item.id%>
            <%= item.to_do%><br />
        <% end %>
    <%end%>
<%end%>

Upvotes: 0

Views: 943

Answers (1)

Milind
Milind

Reputation: 5112

you can do something like.. by using checked property..call ajax using jquery

$('input[type=checkbox][id=to_do]').change(function() {
         var hall_id = $('#to_do:checked').val();
         $.ajax({
         url:"/todo",
         type:'POST',
         //pass any agruments if needed
         //data: {"id": pass any data that you need},
         beforeSend: function (){
                //show the user that something is in progess
                 $("#to_do").parent().html("Submitting..");
                 },
         success:function(data){
         //update user on success/failure
         $("#to_do").parent().html("Submitted");
         }
         });

     }) 

Now,just implement your action on the controller...thats it.

Hope it helps :)

Upvotes: 1

Related Questions