From a4fc4d4961cf29fdde64f2d9575ebf3e8737212c Mon Sep 17 00:00:00 2001 From: songtianlun Date: Tue, 25 Mar 2025 17:21:19 +0800 Subject: [PATCH] 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. --- app/views/arts/index.html.erb | 15 +++++++++++++-- app/views/cities/index.html.erb | 10 +++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/views/arts/index.html.erb b/app/views/arts/index.html.erb index f2fda4f..d1fc094 100644 --- a/app/views/arts/index.html.erb +++ b/app/views/arts/index.html.erb @@ -6,8 +6,12 @@ <% if featured_art&.image&.attached? %>
- <%= 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 %>
<% end %> @@ -107,8 +111,15 @@
<% 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 %> +
+

Something error.

+
+ <% Rails.logger.error("图片加载失败: #{e.message}") if defined?(Rails) %> + <% end %>
diff --git a/app/views/cities/index.html.erb b/app/views/cities/index.html.erb index 79749b9..7a0f802 100644 --- a/app/views/cities/index.html.erb +++ b/app/views/cities/index.html.erb @@ -5,9 +5,13 @@
<% if featured_art&.image&.attached? %> -
- <%= image_tag featured_art.webp_image.processed, - class: "w-full h-full object-cover object-center" %> + <% begin %> +
+ <%= 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 %>
<% end %>