1

我正在研究 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,并带有请求电子邮件的链接。但我很困惑我该如何做到这一点,任何示例或帮助都会有所帮助,谢谢。

4

1 回答 1

1

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

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
于 2012-08-25T08:23:13.160 回答