Post checkbox value using ajax

What is the best way to change a value on database when I change value on checkbox by ajax?

I want to put a checkbox in my list of data:

ex:

<% @posts.each do |post| %>

    <tr">

     <td><%= post.id %></td>

     <td><%= post.title %></td>

  <td>

     <%= button_group do %>

     <%=  pill_button_link_to 'Edit', edit_post_path(post) %>

     <%= pill_negative_trash_button_link_to 'Delete', post, :confirm => 'Are you sure?', :method => :delete %>

<% end %>

 </td>

Then I want to change an boolean value from list of posts, when I change the value of checkbox, without reloading the page.

I’m looking by the best way for do this.

Thanks by help!

What have you tried?

I read about the Observer class, look for javascript observers and tried to find some tutorials or answer about how do it on rails. I find answers, but all about PHP.

What version of rails are you using? Are you using the default
javascript with that version?

I’m using 3.0.9 with jquery-rails

What does this mean: "when I change value on checkbox"? Checkboxes can
either be checked or unchecked. In addition, I don't see any checkboxes
in your view.

when I change value on checkbox => when I checked or unchecked

You still do not see because I’m looking for the best way to put it there.

It sounds like you want to place a checkbox on each object in an index view, then observe the click event in JavaScript within that index view and have the current state of that checkbox sent to an Ajax endpoint in your controller for that model. Does that sound like a good wrap-up?

#foos_controller
def set_status
  @foo = Foo.find(params[:id])
  @foo.update_attribute(:status, params[:status]);
  render :nothing => true
end

#views/foos/index
<%- for foo in @foos %>
<p><%= foo.name %> <%= check_box_tag "foos[#{foo.id}][status]", 1, foo.status, :class => 'status' %></p>
<%- end %>

#Prototype JavaScript
$$('input.status').invoke('observe','click',function(evt){
  new Ajax.Request('/foos/set_status',{parameters: {id: this.id.split('_')[1],status:$F(this)}});
});

Off the top of my head, that ought to work. Of course you'll need to set up a route for set_status that can take a POST.

Walter

is exactly!

Thanks man! It Is a good way.

I will adapt the javascript to jquery!