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

时光怂恿深爱的人放手 提交于 2019-11-29 23:33:37

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.

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'

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!