6

收到此错误,我错过了什么?

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. 我制作了一个新的 Ruby on Rails 应用程序rails new my_app#Rails 6.0.3.2

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

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

  4. 添加包含此内容的静态控制器和索引文件

    应用程序/控制器/static_controller.r b

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

    应用程序/视图/静态/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

4

2 回答 2

4

中的文件public/assets.jpg. 你.jpeg在你的image_tag.

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

<%= image_tag('my_logo.jpg') %>
于 2020-06-28T02:32:22.990 回答
0

我需要有环境变量:RAILS_ENV=production服务器和预编译进程才能工作。

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

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

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

调试模式禁用资产的串联和预处理。

此选项可能会导致视图渲染显着延迟

复杂资产的数量。

此外,@meagar 的回答是正确的-jpeg当所有内容都转换为jpg. 但是如果没有上面的答案,我就无法看到这一点。

于 2020-06-28T04:54:25.517 回答