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>
 <%= 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 %>

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!


Fernando A.

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.

On Mon, Aug 29, 2011 at 9:49 PM, 7stud – [email protected] wrote:

[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Fernando A.

What version of rails are you using? Are you using the default
javascript framework with that version? Check the prototype or jquery
docs depending on what javascript framework you are using.

Because javascript executes on the client side, i.e. in a user’s
browser, it doesn’t matter what server side language is on the other
side of the request, e.g. php, ruby, python, perl, java, etc.

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.

Also, I don’t feel like wasting my time helping you if your question has
already been answered on the other forums you cross posted to.

I’m using 3.0.9 with jquery-rails

On Mon, Aug 29, 2011 at 10:44 PM, 7stud – [email protected] wrote:

To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Fernando A.

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.

On Tue, Aug 30, 2011 at 9:38 AM, 7stud – [email protected] wrote:

To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Fernando A.

[iOSDeveloper] - ObjectiveC
[WebDesigner] - CSS3&HTML5
[WebDeveloper] - RubyOnRails
*--------------------------------------------------------------
*
[portfolio] http://www.fernandoaureliano.com -
[blog]http://www.fernandoaureliano.com/

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 %>

<%= foo.name %> <%= check_box_tag "foos[#{foo.id}][status]", 1, foo.status, :class => 'status' %>

<%- 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!

On Tue, Aug 30, 2011 at 2:51 PM, Walter Lee D.
[email protected]wrote:

end
this.id.split(‘_’)[1],status:$**F(this)}});
when I change value on checkbox => when I checked or unchecked

.
------------------------------------------------------------
.
To unsubscribe from this group, send email to

rubyonrails-talk+unsubscribe@**googlegroups.com[email protected]

.
For more options, visit this group at http://groups.google.com/**

group/rubyonrails-talk?hl=enhttp://groups.google.com/group/rubyonrails-talk?hl=en

.


Fernando A.

[iOSDeveloper] - ObjectiveC
[WebDesigner] - CSS3&HTML5
[WebDeveloper] - RubyOnRails
*--------------------------------------------------------------
*
[portfolio] http://www.fernandoaureliano.com -
[blog]http://www.fernandoaureliano.com/