PDA

View Full Version : Rails + ExtJS



kirkgo
2 Mar 2010, 12:57 PM
How I do to pass ActiveRecord validation errors to ExtJS? (I'm using JSON to comunicate with Ext).

Thanks!

Zyclops
3 Mar 2010, 1:59 PM
object = SomeModel.new(params[:some_model])

if object.save
render :json => {:success => true}
else
render :json = {:success => false, :errors => object.errors}
end


or my preferred method (because it's less code over the controllers):



class SomeController
def some_action
SomeModel.create!(params[:some_model])
json_success
end
end

class ApplicationController
# Simple helper because we have to do this everywhere
def json_success
render :json => {:success => true}
end

rescue_from ActiveRecord::RecordInvalid do |exception|
render :json=> {:success => false, :errors => exception.record.errors)
end
end