i am working on rails3 application. In my application when a user registered first time, an email has been sent to user with a verification link, after clicking that link i update the status_id and redirect user to login page. Here is my code :

code for token generation:

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  

code to send email with link:

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

Email template with verification link:

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>

Now everything is fine, now my client want if user did not get verification email, then we some where give the option or link to request to resend verification mail.

i am thinking on to display flash msg on login attempt with a link to request for email. but i am confused how do i do this any example or help would be helpful thanks.

有帮助吗?

解决方案

Hi friends i got a solution, i have used a method in login controller that check the email is verified or not and if not verified a flash message displayed. The message contains the link . When a user click on that link i resend the verification mail. Here is my code:

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

and in login controller:

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top