收到此错误,我错过了什么?
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" %>
我制作了一个新的 Ruby on Rails 应用程序
rails new my_app
#Rails 6.0.3.2允许它安装 gems、web packer 等。
添加
config.assets.compile = false
到config/environments/development.rb
添加包含此内容的静态控制器和索引文件
应用程序/控制器/static_controller.r b
class StaticController < ApplicationController def index render :index, layout: false end end
应用程序/视图/静态/index.html.erb
<%= image_tag "my_logo.jpg" %>
将 my_logo.jpeg 文件添加到
app/assets/images/
wget https://jpeg.org/images/jpeg2000-home.jpg -O app/assets/images/my_logo.jpg
预编译资产:
bundle exec rake assets:precompile
将 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