本文介绍了gem 更新后:测试失败并显示“资产未声明为在生产中预编译";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我更新了几个 gem 以来,所有测试都失败并显示错误:

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

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

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

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/_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

app/views/layouts/application.html.erb:21:in _app_views_layouts_application_html_erb__4340

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

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") %>

此部分加载在 application.html.erb 中:.

This partial is loaded in application.html.erb: <%= render partial: 'layouts/faviconsheader' %>.

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

Any idea what is causing this error and what to do? Before the gem update all tests passed.

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

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.

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

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.

推荐答案

长答案+解释

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

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.

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

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)

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

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.

简答

在我做了 bundler update 将我的 sprockets-rails 版本从 2.3.3 更改为 3.0.0 之后,我开始发生这种情况.一个简单的解决方法是在 Gemfile 中将 sprockets-rails 恢复到 2.3.3 版并再次运行 bundle install:

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'

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

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.

这篇关于gem 更新后:测试失败并显示“资产未声明为在生产中预编译";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:50
查看更多