资产Pipline,如何将图像路径添加到后台:url()?

iKe*_*ler 0 html css ruby ruby-on-rails ruby-on-rails-4

如何将image_path添加到已编译的CSS?

我必须添加image author.png,我把它放到application.css:

.post-author-icon {

    width: 16px;
    height: 15px;
    background: url("../design/users/author.png")
        no-repeat;
    display: inline-block;
    position: relative;
    top:2px;
}
Run Code Online (Sandbox Code Playgroud)

然后我运行RAILS_ENV=production rake assets:precompile结束获取文件public/assets

assets/application-e1664a39d94f9050096cb42d545f13c8.css
Run Code Online (Sandbox Code Playgroud)

assets/design/users/author-d5ad9ee413cd0b00e2a0d226eac97cc2.png
Run Code Online (Sandbox Code Playgroud)

当然浏览器不加载图像,因为在CSS映像中stil具有相同的名称.

如何解决这个问题?

这是Ruby on Rails v4

dee*_*our 5

您需要利用资产管道处理ERB或SASS的能力.我喜欢SASS,所以我会举一个例子.

  1. 重命名application.cssapplication.css.scss
  2. background样式更改为

    background: image-url("design/users/author.png");
    
    Run Code Online (Sandbox Code Playgroud)

您可以在Rails指南中找到有关此帮助程序和其他帮助程序的更多信息.