ruby-on-rails – 在heroku的rails控制台中的asset_path用于图像

我正在使用回形针来处理图像.

要在用户未提供图像时提供图像,我使用defaul_url提供的值,该值设置为

ActionController::Base.helpers.asset_path('logo_medium.png')

这在本地工作正常:图像的src属性设置正确.

然而,在heroku上,事情就会破裂.图像的src属性值为’/logo_medium.png’,这是错误的.

确实,当我在heroku上启动rails控制台时,

ActionController::Base.helpers.asset_path('logo_medium.png')

回报

/assets/logo_medium-f0bc5e00182468b861cf32c8c35595ce.png

哪个是对的.

我很困惑,应用程序似乎返回了与控制台不同的东西.

为了完整起见,我的观点使用了

<%= image_tag @user.avatar.url(:medium) %>

任何帮助非常感谢.

最佳答案
Asset Fingerprinting

部署到Heroku时,需要预编译资产

这很重要,因为预编译fingerprints your assets,最后给它们一个大的散列值.原因是:

Fingerprinting is a technique that makes the name of a file dependent
on the contents of the file. When the file contents change, the
filename is also changed. For content that is static or infrequently
changed, this provides an easy way to tell whether two versions of a
file are identical, even across different servers or deployment dates.

指纹识别不是Rails的问题;这是您尝试在生产环境中加载这些资产的方式.资产管道只是用于样式化,这意味着如果要调用任何资产,则需要能够使用动态方法来调用它们,例如image_path或asset_path

使用CSS的方法是使用SCSS,使用javascript,它将.erb文件类型附加到JS文件名.这允许您使用动态方法调用所需的资源,从而允许您使用指纹文件

回形针

您在问题的最后添加了Paperclip参考. Paperclip通过ActiveRecord加载文件,并且根本不接触资产

点赞