From e63423573cef49b2102265eef0cb4f74bd29c90a Mon Sep 17 00:00:00 2001 From: Joshua Wehner Date: Tue, 13 Dec 2022 15:15:09 +0100 Subject: [PATCH] Misc/minor article fixes (#18846) * Don't assume all requests are JSON * Prevent nil error from optimized_image_tag (possible local data issue) * Slug miss should 404, instead of Podcast not found * Plain html rescue satisfies spec(s) --- app/controllers/application_controller.rb | 11 ++++++++++- app/controllers/stories_controller.rb | 5 +++-- app/views/articles/_bottom_content.html.erb | 8 +++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6119d66df..3b2f5eb65 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -111,7 +111,16 @@ class ApplicationController < ActionController::Base end def bad_request - render json: { error: I18n.t("application_controller.bad_request") }, status: :bad_request + respond_to do |format| + format.html do + raise if Rails.env.development? + + render plain: "The request could not be understood (400).", status: :bad_request + end + format.json do + render json: { error: I18n.t("application_controller.bad_request") }, status: :bad_request + end + end end def error_too_many_requests(exc) diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 89baa8fe1..6e2f45e43 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -38,10 +38,11 @@ class StoriesController < ApplicationController handle_article_show elsif (@article = Article.find_by(slug: params[:slug])&.decorate) handle_possible_redirect - else - @podcast = Podcast.available.find_by!(slug: params[:username]) + elsif (@podcast = Podcast.available.find_by(slug: params[:username])) @episode = @podcast.podcast_episodes.available.find_by!(slug: params[:slug]) handle_podcast_show + else + not_found end end diff --git a/app/views/articles/_bottom_content.html.erb b/app/views/articles/_bottom_content.html.erb index 91e2735ae..614c9cf8c 100644 --- a/app/views/articles/_bottom_content.html.erb +++ b/app/views/articles/_bottom_content.html.erb @@ -5,9 +5,11 @@
- <%= optimized_image_tag(article.cached_user.profile_image_url, - optimizer_options: { crop: "imagga_scale", width: 100, height: 100 }, - image_options: { loading: "lazy", alt: "#{article.cached_user.username} profile image", class: "crayons-avatar__image" }) %> + <% if article.cached_user.profile_image_url %> + <%= optimized_image_tag(article.cached_user.profile_image_url, + optimizer_options: { crop: "imagga_scale", width: 100, height: 100 }, + image_options: { loading: "lazy", alt: "#{article.cached_user.username} profile image", class: "crayons-avatar__image" }) %> + <% end %>

<%= article.title %>