Rails 4 image-path、image-url 和 assets-url 不再在 SCSS 文件中工作 [英] Rails 4 image-path, image-url and asset-url no longer work in SCSS files
问题描述
除了 image-url
和 Rails 4 中的其他内容之外,我们是否应该使用其他内容?它们返回似乎没有意义的不同值.如果我在 /app/assets/images/logo.png
中有 logo.png
并且我执行以下操作,这就是我得到的:
Are we supposed to use something else aside from image-url
and others in Rails 4? They return different values that don't seem to make sense. If I have logo.png
in /app/assets/images/logo.png
and I do the following, this is what I get:
image-url("logo.png") -> url("/images/logo.png") #obviously doesn't work
image-path("logo.png") -> "/images/logo.png"
asset-url("logo.png") -> url("/logo.png")
当然,这些都不起作用,因为它们至少需要前面的 /assets
.
Of course none of these work because they need at least /assets
in front.
更新:实际上,我刚刚注意到,如何在 Rails 4 中访问图像?我在 /app/assets/images/logo.png
有一张图片.但是,如果我访问以下任何一个 URL,我仍然看不到我的图像:
UPDATE: Actually, I just noticed, how do I access images in Rails 4? I have an image at /app/assets/images/logo.png
. But if I go to any of the following URLs, I still don't see my image:
http://localhost:3000/assets/logo.png
http://localhost:3000/assets/images/logo.png
http://localhost:3000/logo.png
http://localhost:3000/images/logo.png
更新 2:我可以调出我的 logo.png
的唯一方法是将其移动到 /app/assets/stylesheets
目录然后拉起来:
UPDATE 2: The only way I can bring up my logo.png
is by moving it into the /app/assets/stylesheets
directory and then pulling up:
http://localhost:3000/assets/logo.png
推荐答案
我自己也遇到了这个问题.希望有帮助的 3 点:
I just had this issue myself. 3 points that will hopefully help:
- 如果您将图像放在
app/assets/images
目录中,那么您应该能够直接调用图像,路径中没有前缀.IE.image_url('logo.png')
- 取决于您使用资产的位置,取决于方法.如果您将它用作
background-image:
属性,那么您的代码行应该是background-image: image-url('logo.png')
.这适用于 less 和 sass 样式表.如果您在视图中内联使用它,那么您将需要使用 rails 中内置的image_tag
帮助器来输出您的图像.再一次,没有前缀<%= image_tag 'logo.png' %>
- 最后,如果您正在预编译资产,请运行
rake assets:precompile
以生成资产,或运行rake assets:precompile RAILS_ENV=production
以进行生产,否则,您的生产环境在加载页面时不会有指纹资产.
- If you place images in your
app/assets/images
directory, then you should be able to call the image directly with no prefix in the path. ie.image_url('logo.png')
- Depending on where you use the asset will depend on the method. If you are using it as a
background-image:
property, then your line of code should bebackground-image: image-url('logo.png')
. This works for both less and sass stylesheets. If you are using it inline in the view, then you will need to use the built inimage_tag
helper in rails to output your image. Once again, no prefixing<%= image_tag 'logo.png' %>
- Lastly, if you are precompiling your assets, run
rake assets:precompile
to generate your assets, orrake assets:precompile RAILS_ENV=production
for production, otherwise, your production environment will not have the fingerprinted assets when loading the page.
此外,对于第 3 点中的那些命令,如果您正在运行 bundler,则需要在它们前面加上 bundle exec
.
Also for those commands in point 3 you will need to prefix them with bundle exec
if you are running bundler.
这篇关于Rails 4 image-path、image-url 和 assets-url 不再在 SCSS 文件中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!