I have Ajax registration with Rails 5 and Devise. This is
registrations_controller.rb
build_resource(sign_up_params)
byebug
if resource.save
if resource.active_for_authentication?
sign_up(resource_name, resource)
return render :json => {:success => true}
else
expire_data_after_sign_in!
return render :json => {:success => true}
end
else
return render :json => {:success => false}
end
byebug
resource.errors
<ActiveModel::Errors:0x00000007254a48 @base=#<User id: nil, email: "email@email.com", role_id: 0, created_at: nil, updated_at: nil>, @messages={:email=>["has already been taken"]}, @details={:email=>[{:error=>:taken, :value=>"email@email.com"}]}>
resource.errors.messages
{:email=>["has already been taken"]}
if resource.errors.details[:email][0][:error] == :taken
return render :json => {:error=> "Email already in use."}
end
user.rb
validate :email_uniqueness
def email_uniqueness
self.errors.add(:email, 'Email is already in use') if User.where(:email => self.email).exists?
end
This is a default Rails validation error message. Read this http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
You can change the message in *.en.yml
file like this:
en:
activerecord:
errors:
models:
user:
attributes:
email:
taken: is already in use