RoR - 注册后未收到确认电子邮件

时间:2015-04-24 10:13:57

标签: ruby-on-rails ruby email

我尝试在我的rails应用中添加用户确认电子邮件。它工作正常,我输入我的电子邮件,它说"请检查电子邮件确认"但当我检查我的电子邮件/垃圾邮件/垃圾邮件时,没有任何东西存在。但如果我登录,它会说"请确认电子邮件"。

如果我再次注册,则表示已收到电子邮件,但我使用rails c检查了我的用户,但我无法找到该电子邮件或用户。

我的users_controller.rb

def create
  @user = User.create(user_params)

  if @user.save
    UserMailer.registration_confirmation(@user).deliver
    flash[:success] = "Please confirm email"
    redirect_to root_url
  else
    flash[:error] = "Something went wrong..."
    render 'new'
  end
end

def confirm_email
user = User.find_by_confirm_token(params[:id])
if user
  user.email_activate
  flash[:success] = "Welcome to the Sample App! Your email has been confirmed.
  Please sign in to continue."
  redirect_to signin_url
else
  flash[:error] = "Sorry. User does not exist"
  redirect_to root_url
end

private

def user_params
  params.require(:user).permit(:name, :username, :email, :password, :password_confirmation, :avatar_url)
end

end

我的user.rb

require 'digest/md5'

class User < ActiveRecord::Base

before_create :confirmation_token

before_validation :prep_email

    def create_avatar_url
        self.avatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(self.email)}?s=50"
    end

has_secure_password

validates :name, presence: true
validates :username, uniqueness: true, presence: true
validates :email, uniqueness: true, presence: true, format: { with: /\A[\w.+-]+@([\w]+.)+\w+\z/ }

def email_activate
    self.email_confirmed = true
    self.confirm_token = nil
    save!(:validate => false)
end

private

def prep_email
    self.email = self.email.strip.downcase if self.email
end

def confirmation_token
  if self.confirm_token.blank?
      self.confirm_token = SecureRandom.urlsafe_base64.to_s
  end
end

会话控制器

  def create
user = User.find_by_username(params[:username])
if user && user.authenticate(params[:password])
  if user.email_confirmed
    session[:user_id] = user.id
    redirect_to root_url, notice: "Logged in!"
else
    flash.now[:error] = 'Please activate your account by following the 
    instructions in the account confirmation email you received to proceed'
end


else
    flash.now[:error] = "Invalid user/pass"
  end
  end

  def destroy
    session.delete(:user_id)
    redirect_to root_url, notice: "Logged out."
  end

app / mailers / user_mailer.rb

class UserMailer < ActionMailer::Base

default :from => "no-reply@domain.com"

def registration_confirmation(user)
    @user = user
    mail(:to => "#{user.name} <#{user.email}>", :subject => "Registration Confirmation")
end

end

我也在路线文件中添加了这个

  resources :users do
member do
  get :confirm_email
end


 end

还在我的app / views / user_mailer / registration_confirmation.text.erb中添加了text.erb文件

Hi <%= @user.name %>,

Thanks for registering! To confirm your registration click the URL below.

<%= confirm_email_user_url(@user.confirm_token) %>

我真的很感激!谢谢!

0 个答案:

没有答案