Rails5: form remote - trigger submit
I have a form with a remote set of options. I need to program the transmission in software.
In Rails 4 I am using $('form').trigger('submit.rails')
and it works as expected. In Rails 5 (5.1.2 and 5.0.4), triggering this event triggers the standard submit (formdata, not AJAX).
For Rails the staff 'submit.rails' is not part of the 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 %>
source to share
As pointed out in the comment thread, jquery-rails must be included in your file application.js
for things to work properly
The jquery and jquery-ujs files will be added to the asset pipeline and made available to you. If they are still not in app / assets / javascripts / application.js, add the following lines:
//= require jquery //= require jquery_ujs
You seem to be missing the second line
//= require jquery_ujs
After reproducing steps in the GitHub issue and then make sure the line is enabled fixes the issue for me and $('form').trigger('submit.rails')
in the console the javascript starts sending as
PostsController # create as JS handling
source to share