Rails 4.2:仅在config.assets.compile = true时添加图像路径和指纹

use*_*025 5 ruby-on-rails sprockets asset-pipeline

在生产中,不使用图像标记调用图像的正确路径,并且不添加md5指纹.图像名称(例如"pretty_picture.jpg")存储在数据库中.预编译文件都存在于公共文件夹中,包括清单文件.

使用image_tag调用时:

image_tag @test_question.question.image
Run Code Online (Sandbox Code Playgroud)

我明白了:

<img src="/images/pretty_picture.jpg">
Run Code Online (Sandbox Code Playgroud)

如果我在production.rb中设置config.assets.compile = true,则渲染图像,我得到:

<img src="/assets/images/pics/pretty/pretty_picture-e0df5012b6930cda4efaa866af22a63f.jpg" >
Run Code Online (Sandbox Code Playgroud)

我的黑客解决方案是使用(在HAML中)

%img{src: "/assets/"+Rails.application.assets.find_asset(@test_question.question.image).digest_path}
Run Code Online (Sandbox Code Playgroud)

在production.rb我有

config.assets.digest = true
config.assets.enabled = true
config.serve_static_files = false
config.assets.compile = false 
Run Code Online (Sandbox Code Playgroud)

建议不要在生产中将config.assets.compile设置为true.这似乎是代表链轮和资产管道的非常奇怪的行为.知道在这里使用image_tag有什么问题吗?

Bal*_*ick 1

在生产中,您应该在启动服务器之前使用以下命令预编译资产(并在每次部署时自动执行此操作):

rake assets:precompile RAILS_ENV="production"
Run Code Online (Sandbox Code Playgroud)

并保留config.assets.compile = false在您的production.rb. 查看资产管道指南