From f84c6201dd592f9ef0b5e8473951bf6ceec213e6 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Sat, 17 Aug 2013 18:45:50 -0700 Subject: Fail informatively in #respond_with when no appropriate #api_behavior renderer is available. Currently if a user calls #respond_with(csvable), but has not csv renderer available, Responder will just run through the default render behavior twice, raising ActionView::MissingTemplate both times. This changes ActionController::Metal::Responder#api_behavior to check in advance whether there is a renderer available, and raise ActionController::MissingRenderer if not. --- actionpack/lib/action_controller/metal/renderers.rb | 7 +++++++ actionpack/lib/action_controller/metal/responder.rb | 6 ++++++ 2 files changed, 13 insertions(+) (limited to 'actionpack/lib/action_controller/metal') diff --git a/actionpack/lib/action_controller/metal/renderers.rb b/actionpack/lib/action_controller/metal/renderers.rb index 5272dc6cdb..abed6e53cc 100644 --- a/actionpack/lib/action_controller/metal/renderers.rb +++ b/actionpack/lib/action_controller/metal/renderers.rb @@ -6,6 +6,13 @@ module ActionController Renderers.add(key, &block) end + class MissingRenderer < LoadError + def initialize(format) + @format = format + super("No renderer defined for format: #{@format}") + end + end + module Renderers extend ActiveSupport::Concern diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb index fd5b661209..66ff34a794 100644 --- a/actionpack/lib/action_controller/metal/responder.rb +++ b/actionpack/lib/action_controller/metal/responder.rb @@ -202,6 +202,7 @@ module ActionController #:nodoc: # This is the common behavior for formats associated with APIs, such as :xml and :json. def api_behavior(error) raise error unless resourceful? + raise MissingRenderer.new(format) unless has_renderer? if get? display resource @@ -269,6 +270,11 @@ module ActionController #:nodoc: resource.respond_to?(:errors) && !resource.errors.empty? end + # Check whether the neceessary Renderer is available + def has_renderer? + Renderers::RENDERERS.include?(format) + end + # By default, render the :edit action for HTML requests with errors, unless # the verb was POST. # -- cgit v1.2.3