Ruby-on-rails gem更新后:测试失败并显示“资产未声明为在生产中预编译”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34344094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:19:55  来源:igfitidea点击:

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

ruby-on-railsrubyasset-pipelinesprockets

提问by Marty

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

由于我更新了几个 gem,所有测试都失败并显示错误:

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.rband 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

ActionView::Template::Error: Asset 未声明为在生产中预编译。

添加Rails.application.config.assets.precompile += %w( favicons/manifest.json.erb )config/initializers/assets.rb服务器并重新启动

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

应用程序/视图/布局/application.html.erb:21:in _app_views_layouts_application_html_erb__4340

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

该错误似乎是指_faviconsheader.html.erb包含该行的部分:

<%= 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' %>.

此部分加载于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.

知道是什么导致了这个错误以及该怎么做?在 gem 更新之前,所有测试都通过了。

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

我使用 Rails 4.2.5。更新的宝石之一是sprockets(将链轮更新到版本 3.5.2)。我在github上阅读了一些关于链轮 4 有问题的内容,但我没有使用版本 4。



P.S. Even if I add Rails.application.config.assets.precompile += %w( favicons/manifest.json.erb )to config/initializers/assets.rbthe 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.

PS即使我添加Rails.application.config.assets.precompile += %w( favicons/manifest.json.erb )config/initializers/assets.rb错误仍然存​​在。但即使那会奏效,我也想了解为什么会出现这个问题,除了更新一些 gem 之外没有任何更改。

回答by Nathan

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.

我认为正确的解决方法是将文件添加到预编译资产中,如错误消息所建议的那样。也许这不能为您解决问题,因为您有一个需要在运行时呈现的 erb 文件。我想如果该文件是静态 json 文件,那么在将其添加到预编译资产后您仍然不会遇到该问题。

When you use the image_pathhelper, Sprockets is assuming that you've got a static asset. The fact that your app didn't raise errors before sprockets-rails3.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)

当您使用image_path帮助程序时,Sprockets 会假设您有一个静态资产。您的应用程序在sprockets-rails3.0之前没有引发错误这一事实有点令人惊讶。显然,这个新版本在执行标准方面做得更好。(看起来 3.0 还存在其他问题,可能很快会更新)

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

如果您需要在清单中包含 erb,那么最好使用路由路径助手而不是image_pathasset_path获取 url。这将需要您向文件添加清单路由config/routes.rb并通过控制器操作呈现 json 文件。视图文件将是您的.erb清单。



Short Answer

简答

This started happening to me after doing a bundler updatethat changed my sprockets-railsversion from 2.3.3 to 3.0.0. A simple fix is to revert sprockets-railsback to version 2.3.3 in your Gemfile and running bundle installagain:

bundler update将我的sprockets-rails版本从 2.3.3更改为 3.0.0之后,这开始发生在我身上。一个简单的修复是sprockets-rails在你的 Gemfile 中恢复到 2.3.3 版本并bundle install再次运行:

gem 'sprockets-rails', '2.3.3'

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.

顺便说一句:我在开发环境中遇到了这个问题,并且能够通过运行rake assets:precompile. 不幸的是,它没有让我的测试通过。

回答by Yash Agrawal

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

虽然人们给出了很长的答案,但我建议非常简单和简洁的答案 去

config/initializers/assets.rband

config/initializers/assets.rb

Add the following line

添加以下行

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

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

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

其中 style.css 可以替换为您的任何 css 文件名

回答by Multivac

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

我遇到了类似的错误。我没有修改 assets.rb 或任何东西,只是重新启动我的服务器,不再有错误。



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.rband 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'

ActionView::Template::Error(未声明资产在生产中预编译。添加Rails.application.config.assets.precompile += %w( rails.png )config/initializers/assets.rb并重新启动服务器):10:<%= link_to“立即注册!”,'#',类:“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:在`_app_views_static_pages_home_html_erb___1806898863626708249_70312070486240'

回答by gumlym

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

我有一个类似的错误,必须编辑我的 manifest.js 文件才能让它工作。

Edit /assets/config.manifest.jsand then

编辑/assets/config.manifest.js然后

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

Then do a bundle exec rake assets:precompile

然后做一个 bundle exec rake assets:precompile