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

28,535

Solution 1

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.

Solution 2

Though people have given long answers I suggest very simple and concise answer Just go to

config/initializers/assets.rb and

Add the following line

Rails.application.config.assets.precompile += %w( style.css )

where style.css can be replaced by your any file name for css

Solution 3

I got a similar error. I did not modify assets.rb or anything, just restart my server and no error anymore.


ActionView::Template::Error (Asset was not declared to be precompiled in production. Add Rails.application.config.assets.precompile += %w( rails.png ) to config/initializers/assets.rb and restart your server): 10: <%= link_to "Sign up now!", '#', class: "btn btn-lg btn-primary" %> 11: 12: 13: <%= link_to image_tag("rails.png", alt: "Rails logo"), 14: 'http://rubyonrails.org/' %> app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb___1806898863626708249_70312070486240'

Solution 4

I had a similar error and had to edit my manifest.js file in order to get it to work.

Edit /assets/config.manifest.js and then

// manifest.js
//= link_tree ../images
//= link_tree ../stylesheets .css

Then do a bundle exec rake assets:precompile

Share:
28,535

Related videos on Youtube

Marty
Author by

Marty

I'm starting to learn how to program, using Ruby on Rails. I develop my skills while trying to develop my own app idea.

Updated on December 28, 2020

Comments

  • Marty
    Marty over 3 years

    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.

  • Marty
    Marty over 8 years
    Yes, this also for me caused it / solved it. Thanks!
  • Tom Hammond
    Tom Hammond over 8 years
    Sprockets 3.0 did it for me too
  • Yoshi
    Yoshi about 8 years
    in my case, modify Gemfile to gem 'sprockets-rails', '2.3.3' and run bundle update sprockets-rails , then fixed.
  • Nick Schwaderer
    Nick Schwaderer over 7 years
    This comment saved my rear end in August 2016, upgrading to Rails 5 on an older application and the sprockets upgrade broke all the things. Thank you!!!!
  • stevec
    stevec over 3 years
    This worked for me! For some reason, despite my file having the extension .scss, I still had to use this in manifest.js: //= link_tree ../../../vendor/assets/stylesheets .css (note the css extension). I don't know why, but it wouldn't work without that