View Helpers

Propshaft integrates with Rails Action View by providing helper methods to link to your assets from views and layouts. These helpers handle resolving logical asset paths to their final digested URLs.

Generates a <link> tag for one or more CSS stylesheets.

stylesheet_link_tag(*sources, **options)

Examples

Linking a single stylesheet:

<%= stylesheet_link_tag "application" %>
Output:
<link rel="stylesheet" href="/assets/application-a1b2c3d4.css" />

Linking multiple stylesheets:

<%= stylesheet_link_tag "application", "theme", data: { turbo_track: "reload" } %>

Special Symbols for Bulk Inclusion:

Propshaft extends this helper to accept symbols for including multiple assets at once:

  • :all: Includes all CSS files found in the asset load path.
  • :app: Includes only CSS files located under app/assets/stylesheets.
<%# Link to all stylesheets from the app, engines, and gems %>
<%= stylesheet_link_tag :all %>

<%# Link to only the application's stylesheets %>
<%= stylesheet_link_tag :app %>

Subresource Integrity (SRI):

To add an integrity hash for security, set the integrity option to true. See the SRI guide for configuration details.

<%= stylesheet_link_tag "application", integrity: true %>

javascript_include_tag

Generates a <script> tag for one or more JavaScript files.

javascript_include_tag(*sources, **options)

Examples

Linking a single JavaScript file:

<%= javascript_include_tag "application" %>
Output:
<script src="/assets/application-e5f6a7b8.js"></script>

Linking multiple files with options:

<%= javascript_include_tag "application", "stimulus-controllers", defer: true %>

Subresource Integrity (SRI):

Just like stylesheet_link_tag, you can enable SRI with integrity: true.

<%= javascript_include_tag "application", integrity: true %>

compute_asset_path

Resolves a logical asset path to its full, digested URL path. This is useful if you need the asset path directly.

compute_asset_path(path, options = {})

Example

<link rel="preload" href="<%= compute_asset_path 'application.css' %>" as="style">

If the asset is not found, this helper will raise a Propshaft::MissingAssetError.

asset_integrity

Computes the SRI hash for a given asset path. This can be used for custom tags where you need just the integrity hash.

asset_integrity(path, options = {})

Example

<% integrity_hash = asset_integrity('application.js') %>
<script src="..." integrity="<%= integrity_hash %>"></script>