2016-12-05 54 views

回答

1

您應該在您的user型號中添加唯一的電子郵件地址validation

validates :email, uniqueness: true 

此外,重構你的代碼是這樣的:

def create 
    user = User.new(user_params) 
    if user.save 
     flash[:notice] = '#{params[:user][:role]} created successfully' 
     redirect_to users_path(role: params[:role]) 
    else 
     flash[:errors] = "#{user.errors.full_messages.join(', ')}" 
     redirect_to new_user_path 
    end 
end