table user (with id, username, password, is_admin)
my controller (where i adding)
def create
@user = User.new(params[:respondent])
# entered = User.find(:all, :conditions => ["email = ?", @user])
render :json => @user.to_ext_json(:success => @user.save)
return false
end
When i pasting the same email, i got this:
pre>Mysql::Error: Duplicate entry 'sam@sa.com' for key 'email': INSERT INTO `users` (`created_at`, `updated_at`, `email`) VALUES('2011-05-05 09:05:57', '2011-05-05 09:05:57', 'sam@sa.com')</pre>
How to a开发者_运维技巧void this? What i should write in controller (some if?)
Thank you!
you can either validate uniqueness in your model
validates :email, :uniqueness => :true
or you can rescue RecordNotUnique in your controller like so
begin
# create user
rescue ActiveRecord::RecordNotUnique => e
# handle duplicate entry
end
You should not do that in the controller. These are the validations you should be doing in your model. Please read through this guide to understand how to go about it.
精彩评论