4

I have to display checklist of things "to do" (in spanish).

Checklist example

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.

To do this I have the following:

In my view:

<% @user_account.activities.each do |act| %>
   <div class="checklist_check">
      <%= check_box_tag 'activity_status', act.id, act.done, :class => 'activity_status' %> 
      <%= act.name %>
   </div>
<% end %>

In the javascript:

$(function(){
    $(".activity_status").live("change", function(act_id) {
        $.ajax({
            url: "/controller/done",
            beforeSend: function() { alert("Hi") },
            data: "id="+act_id, 
            success: function() { alert('Bye') }
        });
    });
});

In my controller:

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

Also I set up the route for this action: get "controller/done"

The problem:

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.

Any ideas of what am I doing wrong? How would you do it?

Thanks in advance!

Hans
  • 237
  • 5
  • 13

1 Answers1

1

There might be some problems with routing, it would be better if controller is resource, put your done action in collection:

resources :controller do
  collection do
    post 'done' (or get 'done')
  end
end

Also you should try install Firebug and watch for your ajax call, maybe server just don't answer in a right way.

Sandvich
  • 110
  • 1
  • 7
  • Thanks for your answer Sandvich. I tried what you told me but did not make any difference... Maybe there is something wrong with the way I am writing the url... more ideas? Thanks! – Hans Oct 29 '11 at 22:22