Adds a reusable method to render API response on validation failure.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4455 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
ea59d93dc8
commit
d0a3aab2e7
|
@ -414,4 +414,16 @@ class ApplicationController < ActionController::Base
|
||||||
end.to_json
|
end.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -122,8 +122,7 @@ class UsersController < ApplicationController
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { render :action => 'new' }
|
format.html { render :action => 'new' }
|
||||||
format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false }
|
format.api { render_validation_errors(@user) }
|
||||||
format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -180,8 +179,7 @@ class UsersController < ApplicationController
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { render :action => :edit }
|
format.html { render :action => :edit }
|
||||||
format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false }
|
format.api { render_validation_errors(@user) }
|
||||||
format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue ::ActionController::RedirectBackError
|
rescue ::ActionController::RedirectBackError
|
||||||
|
|
Loading…
Reference in New Issue