From fb6133f736455579a131c4568d6aa3c20361dafd Mon Sep 17 00:00:00 2001 From: songtianlun Date: Mon, 10 Mar 2025 23:55:03 +0800 Subject: [PATCH] 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. --- app/views/home/_arts.html.erb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/views/home/_arts.html.erb b/app/views/home/_arts.html.erb index a8302ca..840a6bc 100644 --- a/app/views/home/_arts.html.erb +++ b/app/views/home/_arts.html.erb @@ -3,7 +3,15 @@
<% 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 %> + +
+

Something error.

+
+ <% Rails.logger.error("图片加载失败: #{e.message}") if defined?(Rails) %> + <% end %> <% end %>