From ba16325c966f2c424f9442ed3b22106e324e66c8 Mon Sep 17 00:00:00 2001 From: Niklas Haeusele Date: Thu, 10 Nov 2022 21:47:43 +0100 Subject: [PATCH] Improve the MissingExactTemplate errorpage Passthrough the controller and action name to the error page. --- actionpack/lib/action_controller/metal/exceptions.rb | 8 ++++++++ actionpack/lib/action_controller/metal/implicit_render.rb | 2 +- .../templates/rescues/missing_exact_template.html.erb | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/metal/exceptions.rb b/actionpack/lib/action_controller/metal/exceptions.rb index 8a8818d96a..2c51e5b9e1 100644 --- a/actionpack/lib/action_controller/metal/exceptions.rb +++ b/actionpack/lib/action_controller/metal/exceptions.rb @@ -92,5 +92,13 @@ module ActionController end class MissingExactTemplate < UnknownFormat # :nodoc: + attr_reader :controller, :action_name + + def initialize(message, controller, action_name) + @controller = controller + @action_name = action_name + + super(message) + end end end diff --git a/actionpack/lib/action_controller/metal/implicit_render.rb b/actionpack/lib/action_controller/metal/implicit_render.rb index b2485db5f8..263314f3c2 100644 --- a/actionpack/lib/action_controller/metal/implicit_render.rb +++ b/actionpack/lib/action_controller/metal/implicit_render.rb @@ -42,7 +42,7 @@ module ActionController raise ActionController::UnknownFormat, message elsif interactive_browser_request? message = "#{self.class.name}\##{action_name} is missing a template for request formats: #{request.formats.map(&:to_s).join(',')}" - raise ActionController::MissingExactTemplate, message + raise ActionController::MissingExactTemplate.new(message, self.class, action_name) else logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger super diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb index cc31581f8e..039ff34501 100644 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb @@ -10,8 +10,8 @@ NOTE: Rails usually expects a controller action to render a view template with the same name.

- For example, a BooksController#index action defined in app/controllers/books_controller.rb should have a corresponding view template - in a file named app/views/books/index.html.erb. + For example, a <%= @exception.controller %>#<%= @exception.action_name %> action defined in app/controllers/<%= @exception.controller.controller_path %>_controller.rb should have a corresponding view template + in a file named app/views/<%= @exception.controller.controller_name %>/<%= @exception.action_name %>.html.erb.

However, if this controller is an API endpoint responding with 204 (No Content), which does not require a view template because it doesn't serve an HTML response, then this error will occur when trying to access it with a browser. In this particular scenario, you can ignore this error.