宝石更新后:测试失败,并显示“未声明资产已在生产中预编译". [英] After gem update: test fail with "Asset was not declared to be precompiled in production"

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

问题描述

由于我更新了多个gem,因此所有测试均失败,并显示以下错误:

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

ActionView :: Template :: Error:资产未声明为已预编译 在生产中.

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

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:<%= render partial: 'layouts/faviconsheader' %>.

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(将链轮更新到版本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.

P.S.即使将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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆