Search code examples
ruby-on-railsajaxruby-on-rails-3prototypejsonsubmit

Rails 3: trigger submit on remote form via js/prototype


I'm working on converting and old Rails app from Rails 2.3.14 to Rails 3.0.

It's still using Prototype (converting to jQuery is a later step in our migration path), so no jQuery answers, please.

We've got lots of forms with pulldowns that we currently submit as AJAX whenever one of the pulldowns gets changed.

In the Rails 2.3 version we used form_remote_tag and then the onchange event for the pulldowns would call $('myform').onsubmit(); to submit the form ajaxily.

In the Rails 3.0 branch, we've converted the form to use form_tag with :remote => true, however, when we select a option from the pulldown, Firebug reports $('myform').onsubmit is not a function.

Changing from $('myform').onsubmit(); to just $('myform').submit(); causes the form to get submitted, but not ajaxily.

Any suggestions on how to use the onchange event on the pulldowns to submit the form ajaxily with Prototype and Rails 3?

Thanks!


Solution

  • Figured it out. I simply removed the :remote => true from the form_tag call, and added the following argument in it's place...

    :onsubmit => "new Ajax.Request('#{url}', {asynchronous:true, evalScripts:true, method:'get', parameters:Form.serialize(this)}); return false;"

    That basically reproduces what the old form_remote_tag used to do.

    This isn't intended to be a permanent solution. Just a stepping stone to get us to the point of switching this app over to jQuery.