In a Rails 3 app, I want to make the browser call a remote function whenever a certain checkbox is toggled. In Rails 2, this was easy to do by passing
:onclick => remote_function(...)
to the checkbox helper. In Rails 3, the remote_* functions are deprecated, so I tried the following workaround:
- create a form around the checkbox using
form_tag ... :remote => true
- submit the form by calling
$("dummy_form").submit();
from theonclick
handler
In the rails.js file that comes bundled with Rails is an observer that listens for submit
events. However, these only seem to be triggered when the user clicks on a submit button, but not when form.submit()
is called (so far tested only in FF).
This has the unwanted effect that the submit is then not done in the background via AJAX, but the normal way, so the browser leaves the current site and displays the response from the controller.
Does anyone know a workaround? Maybe a completely different way to get the same functionality?