##// END OF EJS Templates
Adds a reusable method to render API response on validation failure....
Jean-Philippe Lang -
r4341:d0a3aab2e702
parent child
Show More
@@ -414,4 +414,16 class ApplicationController < ActionController::Base
414 414 end.to_json
415 415 end
416 416
417 # Renders API response on validation failure
418 def render_validation_errors(object)
419 options = { :status => :unprocessable_entity, :layout => false }
420 options.merge!(case params[:format]
421 when 'xml'; { :xml => object.errors }
422 when 'json'; { :json => {'errors' => object.errors} } # ActiveResource client compliance
423 else
424 raise "Unknown format #{params[:format]} in #render_validation_errors"
425 end
426 )
427 render options
428 end
417 429 end
@@ -122,8 +122,7 class UsersController < ApplicationController
122 122
123 123 respond_to do |format|
124 124 format.html { render :action => 'new' }
125 format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false }
126 format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false }
125 format.api { render_validation_errors(@user) }
127 126 end
128 127 end
129 128 end
@@ -180,8 +179,7 class UsersController < ApplicationController
180 179
181 180 respond_to do |format|
182 181 format.html { render :action => :edit }
183 format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false }
184 format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false }
182 format.api { render_validation_errors(@user) }
185 183 end
186 184 end
187 185 rescue ::ActionController::RedirectBackError
General Comments 0
You need to be logged in to leave comments. Login now