我不希望原始文件名显示在link_to image_tag中,特别是在图像损坏的情况下。
<%= link_to image_tag(@event.poster.url(:profile), class: 'media-object'), @event.poster.url, target: '_blank' %>
当前,如果图像损坏,则会在其旁边显示文件名。我希望:“活动海报”。谢谢!
我喜欢为这种情况保存默认图像(假设它存储在assets / images.default.png中:]
class Event < ApplicationRecord
has_one :poster
def poster
return super if poster
'default.png'
end
end
如果有海报的URL,将返回其URL;如果没有,将返回默认图像。