fix: handle image load errors in arts view

- Wrap image tag with begin-rescue to catch errors
- Display fallback message when image fails to load
- Log error messages to Rails logger for debugging

This change improves the robustness of the image loading
feature in the arts view by handling potential exceptions
when loading images. Users will see a placeholder message
if the image fails to load, enhancing user experience and
ensuring that the application does not crash due to unhandled
image load errors.
This commit is contained in:
songtianlun 2025-03-10 23:55:03 +08:00
parent 44f446c069
commit fb6133f736

View File

@ -3,7 +3,15 @@
<div class="card bg-base-100 shadow-xl hover:shadow-2xl transition-shadow duration-300">
<figure class="relative aspect-[4/3] overflow-hidden">
<% if art.image.attached? %>
<%= image_tag art.preview_image.processed, class: "w-full h-full object-cover transform hover:scale-105 transition-transform duration-500" %>
<% begin %>
<%= image_tag art.preview_image&.processed, class: "w-full h-full object-cover transform hover:scale-105 transition-transform duration-500" %>
<% rescue StandardError => e %>
<!-- 图片加载失败时的替代显示 -->
<div class="w-full h-full bg-gray-200 flex items-center justify-center">
<p class="text-gray-500">Something error.</p>
</div>
<% Rails.logger.error("图片加载失败: #{e.message}") if defined?(Rails) %>
<% end %>
<% end %>
</figure>
<div class="card-body">