diff options
author | Ben Woosley <ben.woosley@gmail.com> | 2013-08-17 18:45:50 -0700 |
---|---|---|
committer | Ben Woosley <ben.woosley@gmail.com> | 2013-08-17 19:03:45 -0700 |
commit | f84c6201dd592f9ef0b5e8473951bf6ceec213e6 (patch) | |
tree | 509c4639d32ff57d59cc5de65d1b65295c1f845f /actionpack/lib | |
parent | 6776980b9648383c7a4c87d291d7dd8fdce5b2d8 (diff) | |
download | rails-f84c6201dd592f9ef0b5e8473951bf6ceec213e6.tar.gz rails-f84c6201dd592f9ef0b5e8473951bf6ceec213e6.tar.bz2 rails-f84c6201dd592f9ef0b5e8473951bf6ceec213e6.zip |
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.
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_controller/metal/renderers.rb | 7 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/responder.rb | 6 |
2 files changed, 13 insertions, 0 deletions
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 <code>:edit</code> action for HTML requests with errors, unless # the verb was POST. # |