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
This commit is contained in:
Jean-Philippe Lang 2012-03-04 15:16:50 +00:00
parent 5803bf9fc4
commit dc50edae5e
3 changed files with 9 additions and 11 deletions

View File

@ -508,15 +508,8 @@ class ApplicationController < ActionController::Base
# Renders API response on validation failure # Renders API response on validation failure
def render_validation_errors(object) def render_validation_errors(object)
options = { :status => :unprocessable_entity, :layout => false } @error_messages = object.errors.full_messages
options.merge!(case params[:format] render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => false
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
end end
# Overrides #default_template so that the api template # Overrides #default_template so that the api template

View File

@ -0,0 +1,5 @@
api.array :errors do
@error_messages.each do |message|
api.error message
end
end

View File

@ -498,7 +498,7 @@ class ApiTest::IssuesTest < ActionController::IntegrationTest
end end
json = ActiveSupport::JSON.decode(response.body) 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
end end
@ -674,7 +674,7 @@ class ApiTest::IssuesTest < ActionController::IntegrationTest
put '/issues/6.json', @parameters, credentials('jsmith') put '/issues/6.json', @parameters, credentials('jsmith')
json = ActiveSupport::JSON.decode(response.body) 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
end end