Submitting a Remote Form via rails-ujs

797 views Asked by At

I'm slowly modernizing a legacy Rails app, and am trying to swap out jquery-ujs for rails-ujs.

I've installed rails-ujs via the asset pipeline, and know that this is working as expected: in my browser's console, I've got access to the Rails object.

But, when I try to use the Rails.fire event to submit a form, I can't get the expected behavior.

In my view:

<%= form_with model: post, method: :PUT, url: post_update_url(post), local: false, html: { data: { target: "posts.form", action: "ajax:success->posts#submitSuccess" } } do |f| %>
  // Form stuff
<% end %>

In my Stimulus controller:

submit() {
  Rails.fire(this.formTarget, 'submit');
}

submitSuccess() {
  console.log("Success!")
}

If I remove local: false, then the form submits just as expected, and the page re-loads. But as soon as we add AJAX back, Rails.fire() simply returns false. How would I update this to programmatically submit this form remotely?

(Note that my Stimulus controllers are managed via Webpacker, while rails-ujs is installed via the asset pipeline. Due to constraints within my app, I would like to avoid rails-ujs via Webpacker right now, if possible.

0

There are 0 answers