Programmatically Accessing Sprockets Assets

less than 1 minute read

Suppose you have a css or js asset file, and you want to include it in your layout file inline, and not via <script src="..."> or <link href="...">.

You could do so by accessing the asset file’s contents as a ruby string via the Rails.application.assets.find_asset method.

Here’s how to load inline a script file that’s in your asset pipeline path:

<%= Rails.application.assets.find_asset("my-custom-work.min.js").to_s.html_safe %>

And here’s how a css file can be included within a layout. First add a helper method, and then use it in the layout.

def inline_styles(stylesheet)
  <%=raw inline_styles(@stylesheet) %>

Note that since we might’ve added these assets to the precompiled list, we could edit those asset files using our favorite processing language - scss, coffeescript etc.