Вопрос

Currently I have ActionMailer send an email when a user registers, and I generate a random :sign_in_token with the user.

I've also created a method called active to switch the users :registration_complete boolean value to TRUE when they click the link in the email.

How can i set up the url so that the user can click on the link and use my active method to change the users value?

Currently, I am able to send the link and generates a random token, but I don't know how to have the link call my active method.

MODELS

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :sign_in_token, 
              :registration_complete

  ###This generates my sign_in_token
  def generate_sign_in_token
    self.sign_in_token = Digest::SHA1.hexdigest([Time.now, rand].join)
  end

end

CONTROLLER

def create
  @user = RegularUser.new(params[:regular_user])
  if @user.save      

    ###Sends the User an email with sign_in_token
    UserMailer.registration_confirmation(@user, login_url+"/#{@user.sign_in_token}").deliver

    flash[:success] = "Please Check Your Email to Verify your Registration!"
    redirect_to (verifyemail_path)
  else
    render 'new'
  end
end

###method that finds the user and sets the boolean value to true.
def activate
  @user = User.find_by_sign_in_token!(params[:sign_in_token])    
  if @user.update_attribute(registration_complete: true)
    redirect_to login_url
    flash[:success] = "Email has been Verified."
  end 
end

USER_MAILER

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

VIEWS

###Redirects User to Login Page, But how do i connect it to my activate method or user?
<%= link_to "Complete Registration", @login_url %>

ROUTES

match '/login/:sign_in_token', :to => 'sessions#new'
Это было полезно?

Решение

The easiest solution is to simply have the route send them directly to the activate action via post. I would probably set up a completely different route like:

match '/activate/:sign_in_token', :to => 'sessions#activate', via: [:post]

And then send users the link with /activate instead of /login.

Edit: By the way, in your activate method you'll have to add some additional logic to handle the already activated and invalid URL cases.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top