Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After gem update: test fail with "Asset was not declared to be precompiled in production"

Since I updated several gems all tests fail with the error:

ActionView::Template::Error: Asset was not declared to be precompiled in production.

Add Rails.application.config.assets.precompile += %w( favicons/manifest.json.erb ) to config/initializers/assets.rb and restart your server

app/views/layouts/_faviconsheader.html.erb:14:in _app_views_layouts__faviconsheader_html_erb__1320

app/views/layouts/application.html.erb:21:in _app_views_layouts_application_html_erb__4340

The error seems to refer to the partial _faviconsheader.html.erb that includes the line:

<%= content_tag :link, nil, rel: :manifest, href: image_path("favicons/manifest.json.erb") %> 

This partial is loaded in application.html.erb: <%= render partial: 'layouts/faviconsheader' %>.

Any idea what is causing this error and what to do? Before the gem update all tests passed.

I use Rails 4.2.5. One of the gems updated was sprockets (updated sprockets to version 3.5.2). I read something on github about sprockets 4 having a problem, but I'm not using version 4.


P.S. Even if I add Rails.application.config.assets.precompile += %w( favicons/manifest.json.erb ) to config/initializers/assets.rb the error persists. But even if that would have worked I would have wanted to understand why this problem has come about, without any changes except updating some gems.

like image 656
Marty Avatar asked Dec 17 '15 20:12

Marty


1 Answers

Long Answer + Explanation

I think the correct fix is to add the file to the precompiled assets, as recommended by the error message. Maybe that isn't fixing the issue for you because you've got an erb file that needs to be rendered at run time. I imagine if the file was a static json file then you would not still experience the issue after adding it to the precompiled assets.

When you use the image_path helper, Sprockets is assuming that you've got a static asset. The fact that your app didn't raise errors before sprockets-rails 3.0 is somewhat surprising. This new version is doing a better job, apparently, at enforcing the standards. (it also looks like there are other problems with 3.0 that might be updated shortly)

If you need to have erb inside the manifest, then it would be best practice to use a route path helper rather than image_path or asset_path to get the url. This would require you to add a manifest route to your config/routes.rb file and render the json file through a controller action. The view file would be your .erb manifest.


Short Answer

This started happening to me after doing a bundler update that changed my sprockets-rails version from 2.3.3 to 3.0.0. A simple fix is to revert sprockets-rails back to version 2.3.3 in your Gemfile and running bundle install again:

gem 'sprockets-rails', '2.3.3'

As an aside: I was experiencing this issue in development environment and was able to fix it there by running rake assets:precompile. Unfortunately, it didn't get my tests passing.

like image 180
Nathan Avatar answered Sep 20 '22 13:09

Nathan