Sprockets::Rails::Helper::AssetNotFound - 资产 "my_logo.jpeg" 不在资产管道中

Sprockets::Rails::Helper::AssetNotFound - The asset "my_logo.jpeg" is not present in the asset pipeline

收到这个错误,我错过了什么?

Sprockets::Rails::Helper::AssetNotFound in Static#index:

The asset "my_logo.jpg" is not present in the asset pipeline.

ActionView::Template::Error (The asset "my_logo.jpg" is not present in the asset pipeline.):
    1: <%= image_tag "my_logo.jpg" %>
  1. 我在 Rails 应用程序上创建了一个新的 Ruby rails new my_app # Rails 6.0.3.2

  2. 允许它安装 gems、web packer 等。

  3. config.assets.compile = false添加到config/environments/development.rb

  4. 使用此内容添加静态控制器和索引文件

    app/controllers/static_controller.rb

    class StaticController < ApplicationController
      def index
        render :index, layout: false
      end
    end
    

    app/views/static/index.html.erb

    <%= image_tag "my_logo.jpg" %>
    
  5. 将 my_logo.jpeg 文件添加到 app/assets/images/

    wget https://jpeg.org/images/jpeg2000-home.jpg -O app/assets/images/my_logo.jpg
    
  6. 预编译资产:bundle exec rake assets:precompile

  7. 运行 rails 服务器为:bundle exec rails server

我可以看到文件在 public/assets/ 中,名称为:my_logo-484af9fb82bc8a0279ef38b527402b5697475bd8f827251f46fbb182be6c07a1.jpg

如果我将其设置为 true,它会很好地显示图像:config.assets.compile = true

还有来自public/assets/.sprockets-manifest-a7f9f10978778e828273bf0c790645f6.json

的清单文件内容
{
  "files": {
    "manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js": {
      "logical_path": "manifest.js",
      "mtime": "2020-06-28T03:59:01+00:00",
      "size": 2,
      "digest": "75a11da44c802486bc6f65640aa48a730f0f684c5c07a42ba3cd1735eb3fb070",
      "integrity": "sha256-daEdpEyAJIa8b2VkCqSKcw8PaExcB6Qro80XNes/sHA="
    },
    "application-b324c44f04a0d0da658824105489a2676d49df561c3d06723770321fd441977c.css": {
      "logical_path": "application.css",
      "mtime": "2020-06-28T03:59:01+00:00",
      "size": 675,
      "digest": "4998ce12ecefa6ba42de36e4beac458527529608f8cf0fe6c97acd87850045e4",
      "integrity": "sha256-SZjOEuzvprpC3jbkvqxFhSdSlgj4zw/myXrNh4UAReQ="
    },
    "my_logo-484af9fb82bc8a0279ef38b527402b5697475bd8f827251f46fbb182be6c07a1.jpg": {
      "logical_path": "my_logo.jpg",
      "mtime": "2020-06-28T03:59:01+00:00",
      "size": 20974,
      "digest": "acda9fe5166e93aef6ef9572c07e9a6bf3f4879180765b3d95d9a631b852191c",
      "integrity": "sha256-rNqf5RZuk67275VywH6aa/P0h5GAdls9ldmmMbhSGRw="
    }
  },
  "assets": {
    "manifest.js": "manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js",
    "application.css": "application-b324c44f04a0d0da658824105489a2676d49df561c3d06723770321fd441977c.css",
    "my_logo.jpg": "my_logo-484af9fb82bc8a0279ef38b527402b5697475bd8f827251f46fbb182be6c07a1.jpg"
  }
}

我还打开了 config.assets.unknown_asset_fallback = true 来查看路径是什么,结果是:http://localhost:3000/images/my_logo.jpg 当它为假时(当它工作时),我得到这条路径:http://localhost:3000/assets/my_logo-484af9fb82bc8a0279ef38b527402b5697475bd8f827251f46fbb182be6c07a1.jpg

public/assets 中的文件以 .jpg 结尾。您正在 image_tag.

中使用 .jpeg

您需要重命名您的源图像以 .jpg 结尾,并更新您的 image_tag 调用:

<%= image_tag('my_logo.jpg') %>

我需要环境变量:RAILS_ENV=production 以便服务器和预编译进程正常工作。

我找到了我需要用 RAILS_ENV=production 运行 的唯一原因,因为文件:config/environments/development.rb 将此设置为 true:config.assets.debug = true

如果将其设置为 false:config.assets.debug = false - 那么您也可以在开发中使用预编译资产。

我应该在源代码中阅读它上面的评论:

Debug mode disables concatenation and preprocessing of assets.

This option may cause significant delays in view rendering with a large

number of complex assets.

此外, - 当所有内容都转换为 jpg 时,不要使用 jpeg 扩展名。但是如果没有上面的答案我就看不到了。