{ attribute => error }
end.to_json
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
respond_to do |format|
format.html { render :action => 'new' }
- format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false }
- format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false }
+ format.api { render_validation_errors(@user) }
end
end
end
respond_to do |format|
format.html { render :action => :edit }
- format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false }
- format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false }
+ format.api { render_validation_errors(@user) }
end
end
rescue ::ActionController::RedirectBackError