fix: handle image loading errors in views

- Wrap image_tag calls in begin-rescue blocks to catch loading errors
- Log any exceptions to Rails logger for better debugging
- Provide user feedback with a placeholder when images fail to load

These changes improve the robustness of the application by ensuring that errors do not cause disruptions in the user interface.
This commit is contained in:
songtianlun 2025-03-25 17:21:19 +08:00
parent b33552f6b1
commit a4fc4d4961
2 changed files with 20 additions and 5 deletions

View File

@ -6,8 +6,12 @@
<!-- 背景图像 -->
<% if featured_art&.image&.attached? %>
<div class="absolute inset-0 h-[40vh] overflow-hidden">
<%= image_tag featured_art.webp_image.processed,
<% begin %>
<%= image_tag featured_art.webp_image.processed,
class: "w-full h-full object-cover" %>
<% rescue StandardError => e %>
<% Rails.logger.error("image load error: #{e.message}") if defined?(Rails) %>
<% end %>
<div class="absolute inset-0 bg-gradient-to-b from-base-100/30 via-base-100/60 to-base-100"></div>
</div>
<% end %>
@ -107,8 +111,15 @@
<div class="card bg-base-100 shadow-xl hover:shadow-2xl transition-all duration-300 group overflow-hidden">
<figure class="relative aspect-square overflow-hidden">
<% if art.image.attached? %>
<%= image_tag art.preview_image.processed,
<% begin %>
<%= image_tag art.preview_image.processed,
class: "w-full h-full object-cover transform group-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 %>
<div class="absolute inset-0 bg-gradient-to-t from-black via-black/50 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300"></div>

View File

@ -5,9 +5,13 @@
<div class="relative bg-base-100">
<!-- 背景图像和渐变 -->
<% if featured_art&.image&.attached? %>
<div class="absolute inset-0 h-[60vh] overflow-hidden">
<%= image_tag featured_art.webp_image.processed,
class: "w-full h-full object-cover object-center" %>
<% begin %>
<div class="absolute inset-0 h-[60vh] overflow-hidden">
<%= image_tag featured_art.webp_image.processed,
class: "w-full h-full object-cover object-center" %>
<% rescue StandardError => e %>
<% Rails.logger.error("image load error: #{e.message}") if defined?(Rails) %>
<% end %>
<div class="absolute inset-0 bg-gradient-to-b from-base-100/40 via-base-100/80 to-base-100"></div>
</div>
<% end %>