不要在 Heroku 上显示背景图片。但是本地正在工作
Don't show background images on Heroku. But local is working
我的标签:
= link_to '', root_path, class: 'items__footage'
我的选择器:
.items__footage {
display: inline-block;
content: '';
width: 300px;
min-height: 300px;
background: url('/assets/footage_still.png') no-repeat;
background-size: auto 100%;
}
我的图片路径是assets/images/footage_still.png
。如果我指定这个路径,我的图像就不能在本地工作。
我已经完成了rake assets:precompile
。但这并没有帮助。
可以使用image_url('footage_still.png')
让rails找到图片的路径
In the production environment Sprockets uses the fingerprinting scheme
outlined above. By default Rails assumes assets have been precompiled
and will be served as static assets by your web server.
During the precompilation phase an SHA256 is generated from the
contents of the compiled files, and inserted into the filenames as
they are written to disk. These fingerprinted names are used by the
Rails helpers in place of the manifest name.
因此,在生产环境中,您的所有资产都将被预编译,并由 public/assets
提供。此外,该文件将使用指纹重命名。所以实际上在生产中你的文件名看起来像
footage_still-908e25f4bf641868d8683022a5b62f54.png
指纹会随着文件内容的变化而变化,用于缓存静态资源,一般称为cache busting
。
因此,当您对图像 url /assets/footage_still.png
进行硬编码时,它会在生产中中断。为了处理这种情况,rails 提供了一个叫做 asset url helpers 的东西。
要使其正常工作,您必须将 .css
文件重命名为 .scss
并更改。
background: url('/assets/footage_still.png') no-repeat;
至
background: image_url('footage_still.png') no-repeat;
希望这对您有所帮助。
如果您可以在 css 文件中使用 ruby 代码,则可以像这样使用 items_footage 的背景。
background: url("<%= asset_path('footage_still.png') %>");
要在 css 文件中使用 ruby 代码,请将该文件设为 filename.css.erb
我的标签:
= link_to '', root_path, class: 'items__footage'
我的选择器:
.items__footage {
display: inline-block;
content: '';
width: 300px;
min-height: 300px;
background: url('/assets/footage_still.png') no-repeat;
background-size: auto 100%;
}
我的图片路径是assets/images/footage_still.png
。如果我指定这个路径,我的图像就不能在本地工作。
我已经完成了rake assets:precompile
。但这并没有帮助。
可以使用image_url('footage_still.png')
让rails找到图片的路径
In the production environment Sprockets uses the fingerprinting scheme outlined above. By default Rails assumes assets have been precompiled and will be served as static assets by your web server.
During the precompilation phase an SHA256 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disk. These fingerprinted names are used by the Rails helpers in place of the manifest name.
因此,在生产环境中,您的所有资产都将被预编译,并由 public/assets
提供。此外,该文件将使用指纹重命名。所以实际上在生产中你的文件名看起来像
footage_still-908e25f4bf641868d8683022a5b62f54.png
指纹会随着文件内容的变化而变化,用于缓存静态资源,一般称为cache busting
。
因此,当您对图像 url /assets/footage_still.png
进行硬编码时,它会在生产中中断。为了处理这种情况,rails 提供了一个叫做 asset url helpers 的东西。
要使其正常工作,您必须将 .css
文件重命名为 .scss
并更改。
background: url('/assets/footage_still.png') no-repeat;
至
background: image_url('footage_still.png') no-repeat;
希望这对您有所帮助。
如果您可以在 css 文件中使用 ruby 代码,则可以像这样使用 items_footage 的背景。
background: url("<%= asset_path('footage_still.png') %>");
要在 css 文件中使用 ruby 代码,请将该文件设为 filename.css.erb