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

后端 未结 4 861
臣服心动
臣服心动 2020-12-24 10:41

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

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

A

相关标签:
4条回答
  • 2020-12-24 11:21

    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'

    0 讨论(0)
  • 2020-12-24 11:26

    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

    0 讨论(0)
  • 2020-12-24 11:41

    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.

    0 讨论(0)
  • 2020-12-24 11:42

    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

    0 讨论(0)
提交回复
热议问题