如何给用户重新发送验证电子邮件的选项?

最后发布: 2012-08-24 05:33:27


问题

我正在使用rails3应用程序。 在我的应用程序中,当用户首次注册时,已经通过验证链接向用户发送了一封电子邮件,单击该链接后,我将更新status_id并将用户重定向到登录页面。 这是我的代码:

令牌生成的代码:

require 'digest/sha2'
class Subscription < ActiveRecord::Base


  validate :ids_must_be_present, :on => :create 

 def ids_must_be_present
    if status_id==0 
      generate_token
    else
      errors.add('Something gone wrong')
    end
  end

  def generate_token
    self.token = encrypt_string(id, generate_salt)
  end

  def encrypt_string(id, salt)
    Digest::SHA2.hexdigest(id.to_s + "prftnxt" + salt)
  end



  private

  def generate_salt
    self.object_id.to_s + rand.to_s + company_id.to_s  + Time.now.to_i.to_s
  end
end  

发送带有链接的电子邮件的代码:

def email_verify
   if subscription = Subscription.find_by_id_and_token(params[:id], params[:token])
      subscription.update_attribute(:status_id, 1)
      redirect_to("/login/index", :notice => "Thanks, email successfully verified")
   else
     flash.now[:notice] = "Your email has not verified yet. Please verify your email by clicking the link we have sent you."
   end
  end

带有验证链接的电子邮件模板:

Hello <b><%= @user.username %></b>,
<br/>
<br/>
Thank you for signing up .
<b> Please Verify your email</b>

    <%= link_to "verify", "http://localhost:3000/login/email_verify?token=#{@subscription.token}&id=#{@subscription.id}"%>

</br></br>
</br>

现在一切都很好,现在我的客户希望如果用户没有收到验证电子邮件,那么我们在某些地方提供选项或链接以请求重新发送验证邮件。

我正在考虑在登录尝试时显示Flash消息,并带有一个请求电子邮件的链接。 但我感到困惑,我该如何做任何示例或帮助会有所帮助,谢谢。

ruby-on-rails-3 email-verification
回答

嗨,朋友,我有一个解决方案,我在登录控制器中使用了一种方法来检查电子邮件是否已验证,如果未验证,则显示一条闪断消息。 该消息包含链接。 当用户单击该链接时,我会重新发送验证邮件。 这是我的代码:

subscription = Subscription.find_by_company_id_and_plan_id(current_company.id, current_company.plan.id)
      link = "<a href= '/login/resend_verification_email'>Click</a>"
      if subscription.status_id == 0
         flash[:error] = "Your email is not verified. Please verify before login. <br/> #{link} here to resend verification email.".html_safe 
         redirect_to :back
      end

并在登录控制器中:

def resend_verification_email
    subscription = Subscription.find_by_company_id_and_plan_id(current_company.id, current_company.plan.id)
    Email.verify_email(current_user, subscription).deliver
    redirect_to :back
    flash[:success] = 'Verification email has been resend successfully, please check your inbox.' 
  end