Using Rails-UJS in JS modules (Rails 6 with webpacker) : <p>i just switched to Rails 6 (6.0.0.rc1) which uses the <a href="https://github.com/rails/webpacker" rel="noreferrer">Webpacker</a> gem by default for Javascript assets together with Rails-UJS. I want to use Rails UJS in some of my modules in order to submit forms from a function with:</p>

<pre><code>const form = document.querySelector("form")
Rails.fire(form, "submit")
</code></pre>

<p>In former Rails versions with Webpacker installed, the <code>Rails</code> reference seemed to be "globally" available in my modules, but now i get this when calling <code>Rails.fire</code>…</p>

<pre><code>ReferenceError: Rails is not defined
</code></pre>

<p><strong>How can i make <code>Rails</code> from <code>@rails/ujs</code> available to a specific or to all of my modules?</strong></p>

<p>Below my setup…</p>

<p><em>app/javascript/controllers/form_controller.js</em></p>

<pre><code>import { Controller } from "stimulus"

export default class extends Controller {
  // ...
  submit() {
    const form = this.element
    Rails.fire(form, "submit")
  }
  // ...
}
</code></pre>

<p><em>app/javascript/controllers.js</em></p>

<pre><code>// Load all the controllers within this directory and all subdirectories. 
// Controller files must be named *_controller.js.

import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"

const application = Application.start()
const context = require.context("controllers", true, /_controller\.js$/)
application.load(definitionsFromContext(context))
</code></pre>

<p><em>app/javascript/packs/application.js</em></p>

<pre><code>require("@rails/ujs").start()
import "controllers"
</code></pre>

<p>Thanks!</p>
