@@ -413,5 +413,17 class ApplicationController < ActionController::Base | |||||
413 | { attribute => error } |
|
413 | { attribute => error } | |
414 | end.to_json |
|
414 | end.to_json | |
415 | end |
|
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 | end |
|
429 | end |
@@ -122,8 +122,7 class UsersController < ApplicationController | |||||
122 |
|
122 | |||
123 | respond_to do |format| |
|
123 | respond_to do |format| | |
124 | format.html { render :action => 'new' } |
|
124 | format.html { render :action => 'new' } | |
125 | format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false } |
|
125 | format.api { render_validation_errors(@user) } | |
126 | format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false } |
|
|||
127 | end |
|
126 | end | |
128 | end |
|
127 | end | |
129 | end |
|
128 | end | |
@@ -180,8 +179,7 class UsersController < ApplicationController | |||||
180 |
|
179 | |||
181 | respond_to do |format| |
|
180 | respond_to do |format| | |
182 | format.html { render :action => :edit } |
|
181 | format.html { render :action => :edit } | |
183 | format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false } |
|
182 | format.api { render_validation_errors(@user) } | |
184 | format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false } |
|
|||
185 | end |
|
183 | end | |
186 | end |
|
184 | end | |
187 | rescue ::ActionController::RedirectBackError |
|
185 | rescue ::ActionController::RedirectBackError |
General Comments 0
You need to be logged in to leave comments.
Login now