5

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 %>
12
  • 1
    I've never heard of submit.rails. where does it come from? Commented Jul 19, 2017 at 6:48
  • 2
    Seems like this is a part of jquery-rails. Can you ensure that this is included in your project? Commented Jul 19, 2017 at 7:07
  • Yes, it's included in my project Commented Jul 19, 2017 at 7:20
  • Why don't just use $('form').submit() Commented Jul 19, 2017 at 7:22
  • @DeepakMahakale because it causes a standard submit (formdata, not AJAX) Commented Jul 19, 2017 at 7:23

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.