Reputation: 35
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 '[email protected]' for key 'email': INSERT INTO `users` (`created_at`, `updated_at`, `email`) VALUES('2011-05-05 09:05:57', '2011-05-05 09:05:57', '[email protected]')</pre>
How to avoid this? What i should write in controller (some if?)
Thank you!
Upvotes: 1
Views: 2289
Reputation: 8807
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.
Upvotes: 1
Reputation: 901
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
Upvotes: 1