1
我想使用我的用戶控制器來通知用戶,當他們的電子郵件已被用於註冊,但即使當電子郵件已存在,我仍然得到錯誤「Plase驗證您的輸入並重試「,而不是」您已經註冊!感謝您的熱心!「使用控制器而不是實現這種行爲的創建方式?使用閃光燈通知用戶以前的註冊
在rails控制檯(假設「[email protected]」在數據庫中),當我使用user = User.new(name:"Example", email:"[email protected]")
然後User.find_by_email(user.email)
它確實返回了正確的User條目,所以我不確定我是否在正確的軌道,只是執行它錯誤地或什麼任何想法
users_controller.rb:?
class UsersController < ApplicationController
def new
@user = User.new(params[:user])
end
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Thanks for supporting cofind! We'll be in touch!"
redirect_to root_path
UserMailer.welcome_email(@user).deliver
else
if @user.email == User.find_by_email(@user.email)
flash[:error] = "You've already registered! Thanks for being enthusiastic!"
redirect_to root_path
else
flash[:error] = "Plase validate your input and try again."
redirect_to signup_path
end
end
end
end
user.rb:
class User < ActiveRecord::Base
attr_accessible :email, :name
before_save { |user| user.email = email.downcase }
validates :name, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
end
謝謝!我應該知道它會是如此簡單! – 2013-03-19 02:04:42