submit a rails remote form with javascript

In Rails 5.1+, which replaces the old jquery-ujs with a non-jquery rails-ujs, the above answers no longer work, always submitting the form via an HTML HTTP request. This is how you trigger the new rails submit event handler:

var elem = document.getElementById('myform') // or $('#myform')[0] with jQuery
Rails.fire(elem, 'submit');

(Can’t tell you how long it took me to figure that one out…) For some reason, the regular events don’t bubble up properly to the delegated event handler attached by rails using rails’ new delegate function, when they are triggered by jQuery.

Leave a Comment