ActionView::Helpers::AssetTagHelper

javascript_include_tag

API docs (Rails 8.1.4.1) ↗
def javascript_include_tag(*sources)

Description

Creates a <script> that loads a JavaScript file.

Arguments

Changing a value updates the ERB and HTML.

Used as the src attribute. Depending on the value:
  • A URL (a string starting with https:// or another …://, //, data: or cid:): used as is.
  • A string starting with / ("/legacy.js"): used as is.
  • Any other string ("application"): looks up the asset (a file in app/assets) and uses its path, with a digest added to the file name. If the asset is not found, raises Propshaft::MissingAssetError. The lookup and digest come from Propshaft; in an app without an asset pipeline, it just prepends /javascripts/ to the path.
Unless it is a URL, the trailing .js can be left out ("/legacy" becomes /legacy.js).
= nil
The second and later sources. Any number can be given; each gets its own <script>, joined with newlines. This page lets you try up to the second.
options= {}
true adds defer="defer".
"module"
Sets the type attribute.
true adds crossorigin="anonymous". A string sets the crossorigin attribute.
"sha384-abc"
Accepts:
  • A string: sets the integrity attribute as is.
  • true: sets it to the asset hash that Propshaft computes. This site does not configure the hash algorithm (config.assets.integrity_hash_algorithm), so nothing is added.
Propshaft adds no integrity attribute, even for a string, to a request that is neither https nor local.
"cdn.example.com"
Prepends the given host to the path, using http or https to match the current request.
true skips the asset lookup and prepends /javascripts/ to the path.
true sets nonce to the CSP nonce. This site does not configure CSP, so nothing is added. false omits the nonce attribute.
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <script>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other options become HTML attributes of the <script>; those with a nil value are left out.

ERB

Changes from the previous output: Added Removed Changed


          

HTML