首页 文章

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

提问于
浏览
6

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

使用image_tag调用时:

image_tag @test_question.question.image

我明白了:

<img src="/images/pretty_picture.jpg">

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

<img src="/assets/images/pics/pretty/pretty_picture-e0df5012b6930cda4efaa866af22a63f.jpg" >

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

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

在production.rb我有

config.assets.digest = true
config.assets.enabled = true
config.serve_static_files = false
config.assets.compile = false

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

1 回答

  • 1

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

    rake assets:precompile RAILS_ENV="production"
    

    并在 production.rb 中保留 config.assets.compile = false . 检查Asset Pipeline Guide .

相关问题