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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3df897745c664e3add6f047daa5835d1?d=identicon&s=25 MenDAKE (Guest)
on 2007-04-12 19: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?
3df897745c664e3add6f047daa5835d1?d=identicon&s=25 MenDAKE (Guest)
on 2007-04-12 20: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") %>

</form>
This topic is locked and can not be replied to.