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

时间:2012-08-24 05:33:27

标签: ruby-on-rails-3 email-verification

我正在使用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 msg以及请求电子邮件的链接。 但我很困惑,我怎么做这个任何例子或帮助将有所帮助谢谢。

1 个答案:

答案 0 :(得分:1)

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

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