I have a form with remote option set. I need to trigger the submit programmatically.
In Rails 4 I use $('form').trigger('submit.rails') and it works as expected.
In Rails 5 (5.1.2 and 5.0.4) triggering that event causes a standard submit (formdata, not AJAX).
For Rails staff 'submit.rails' is not part of Rails project, see here
Any idea?
UPDATE:
Form code:
<%= form_with(model: post, remote: true) do |form| %>
  <% if post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
      <ul>
      <% post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>
                As stated in the comments thread, the jquery-rails gem needs to be included into your application.js file for things to work properly
The jquery and jquery-ujs files will be added to the asset pipeline and available for you to use. If they're not already in app/assets/javascripts/application.js by default, add these lines:
//= require jquery //= require jquery_ujs
It appears you're missing the second line
//= require jquery_ujs
Following your reproduction steps in the GitHub issue, and then making sure that line is included fixes the issue for me, and $('form').trigger('submit.rails') in the javascript console starts submitting as
Processing by PostsController#create as JS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With