ruby-on-rails - 隐藏文件名(或显示默认的通用文件名),尤其是在 link_to image_tag 图像损坏的情况下
问题描述
我不希望原始文件名显示在 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(如果有),如果没有则返回默认图像。
推荐阅读
- python - keras 适合 y=None 和嵌入层
- numpy - Julia中的批量矩阵乘法
- dafny - 使用工厂模式时,Dafny 调用可能违反上下文的修改子句
- sql - SQL 问题,关于从现有表中删除新表中的记录
- swift - Swift:线程 10:致命错误:无法解析
- python - 设置 selenium debian 服务器
- tsql - 为包含子查询的连接起别名的正确语法是什么?
- arrays - 如何基于布尔值将numpy数组映射到不同大小的数组,其余为零
- laravel - 按钮不重定向
- java - Apache Storm Supervisor 显示:kill:向 5517 发送信号失败:没有这样的进程