Search code examples
javascriptruby-on-railsrubyasset-pipelineassets

Rails assets not available on production


I have this specific issue. On a (LARGE) rails setup I have a backbone project in /app/assets/reader/. All of my javascript assets are precompiling dynamically into reader.js, this works fine. My i10n files in locale/ don't play nice however because they don't need any precompiling. In development it works fine, but in production they are not available.

In my /app/views/layouts/reader.html.erb file I have the following lines:

<%= javascript_include_tag "reader" %>
<%= javascript_include_tag "locale/en" %>

The problem is that the lower one results in a 404 error on production.

I've tried the following alternatives:

<%= javascript_include_tag "en" %>
<%= javascript_include_tag "locale/en" %>
<%= javascript_include_tag "reader/locale/en" %>

None of these seem work. The last one even broke on development.

PS: in applicaton.rb I have:

...
config.assets.precompile += [
    ...
    'reader.js',
    ...
]
...
config.assets.paths << File.join(Rails.root, 'app', 'assets', 'reader', 'locale')

Solution

  • The solution was not in the javascript_include_tag, but rather in the way config.assets.precompile was formatted.

    By default Rails scans for any subfolder DIRECTLY within assets. That meant that the locale file had to be added as locale/en.js to config.assets.precompile, and that the config.assets.paths line was not even necessary at all.

    The way rails scans subfolder is really specific and important. Get that right, and it should all work like a breeze. Once you know how it works, it actually gets quite powerful.

    Pro tip: I ended up using locale/*.js in combination with <%= javascript_include_tag "locale/#{I18n.locale}" %>, as I actually have a lot of locale files.