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

安稳与你 提交于 2019-11-28 20:50:20

问题


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.


回答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.




回答2:


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'




回答3:


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



来源:https://stackoverflow.com/questions/34344094/after-gem-update-test-fail-with-asset-was-not-declared-to-be-precompiled-in-pr

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