Forum: Ruby on Rails Submit a remote form with a checkbox.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
MenDAKE (Guest)
on 2007-04-12 21:17
I'm having a difficult time figuring out how to submit a remote (AJAX)
form using the onclick of a checkbox. A normal submit button works fine,
but the following statement submits the form normally, seemingly
bypassing AJAX altogether:

<%= check_box "report", "item", :onclick => "this.form.submit()" %>

I can't simply use a remote_function because I need the form to actually
submit so that the other fields on the form can be saved. How can I go
about this?
MenDAKE (Guest)
on 2007-04-12 22:13
Problem solved. For those interested in the solution, it turns out that
the problem is a fundamental javascript issue, where
onclick="this.form.submit()" completely ignores a form's "onsubmit"
event. The solution is to put the entire ajax command in the onclick of
your checkbox.

So it turns out remote_function was the way to go afterall. The trick is
to give the form a DOM element id and then add the :submit parameter to
the remote_function, pointing to the form's id. Here's an example of how
it works:

<%= form_remote_tag :update => "some_div", :url => { :action => "save"
}, :html => { :id => "dom_id"  } %>

<%= check_box "report", "word", :onclick => remote_function(:update =>
"some_div", :url => { :action => "save" }, :submit => "dom_id") %>

This topic is locked and can not be replied to.