隐藏文件名(或显示默认的通用文件名),尤其是当link_to image_tag图像损坏时

问题描述 投票:0回答:1

我不希望原始文件名显示在link_to image_tag中,特别是在图像损坏的情况下。

<%= link_to image_tag(@event.poster.url(:profile), class: 'media-object'), @event.poster.url, target: '_blank' %>

当前,如果图像损坏,则会在其旁边显示文件名。我希望:“活动海报”。谢谢!

ruby-on-rails link-to
1个回答
0
投票

我喜欢为这种情况保存默认图像(假设它存储在assets / images.default.png中:]

class Event < ApplicationRecord
  has_one :poster

  def poster
    return super if poster
    'default.png'
  end
end

如果有海报的URL,将返回其URL;如果没有,将返回默认图像。

© www.soinside.com 2019 - 2024. All rights reserved.