From dc50edae5e27fdfc72a7de4d9b7021d98d9621bc Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 4 Mar 2012 15:16:50 +0000 Subject: [PATCH] Adds a template for API error messages so that it does not depend on AR::Errors serialization. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9094 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/application_controller.rb | 11 ++--------- app/views/common/error_messages.api.rsb | 5 +++++ test/integration/api_test/issues_test.rb | 4 ++-- 3 files changed, 9 insertions(+), 11 deletions(-) create mode 100644 app/views/common/error_messages.api.rsb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d6cf2c979..24b45f6ac 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -508,15 +508,8 @@ class ApplicationController < ActionController::Base # Renders API response on validation failure def render_validation_errors(object) - options = { :status => :unprocessable_entity, :layout => false } - options.merge!(case params[:format] - when 'xml'; { :xml => object.errors } - when 'json'; { :json => {'errors' => object.errors} } # ActiveResource client compliance - else - raise "Unknown format #{params[:format]} in #render_validation_errors" - end - ) - render options + @error_messages = object.errors.full_messages + render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => false end # Overrides #default_template so that the api template diff --git a/app/views/common/error_messages.api.rsb b/app/views/common/error_messages.api.rsb new file mode 100644 index 000000000..811d2a863 --- /dev/null +++ b/app/views/common/error_messages.api.rsb @@ -0,0 +1,5 @@ +api.array :errors do + @error_messages.each do |message| + api.error message + end +end diff --git a/test/integration/api_test/issues_test.rb b/test/integration/api_test/issues_test.rb index 4445a000d..a092190d6 100644 --- a/test/integration/api_test/issues_test.rb +++ b/test/integration/api_test/issues_test.rb @@ -498,7 +498,7 @@ class ApiTest::IssuesTest < ActionController::IntegrationTest end json = ActiveSupport::JSON.decode(response.body) - assert json['errors'].include?(['subject', "can't be blank"]) + assert json['errors'].include?("Subject can't be blank") end end @@ -674,7 +674,7 @@ class ApiTest::IssuesTest < ActionController::IntegrationTest put '/issues/6.json', @parameters, credentials('jsmith') json = ActiveSupport::JSON.decode(response.body) - assert json['errors'].include?(['subject', "can't be blank"]) + assert json['errors'].include?("Subject can't be blank") end end