Rails 4的图像路径,图像URL和资产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点中的那些命令,如果您正在运行捆绑程序,则还需要给它们加上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的图像路径,图像URL和资产URL在SCSS文件中不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!