I am using Hartl's Tutorial for Account Activation
user.rb
attr_accessor :activation_token
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
Once the update action shown below in my UsersController is complete and the user is redirected to the root_url
def update
@user = User.find(params[:id])
...
elsif !params[:user][:email].blank?
if @user.authenticate(params[:user][:current_password])
@user.update_attributes(email_user_params)
if @user.save
@user.create_activation_digest
@user.deactivated
@user.send_activation_email
log_out
flash[:info] = "Please check email dude"
redirect_to root_url
else
flash[:danger] = "Email Update Failed"
redirect_to edit_user_email_path(@user)
end
else
flash[:danger] = "Current Password Is Incorrect"
redirect_to edit_user_email_path(@user)
end
...
def edit
@user = User.find(params[:id])
end
then:
:activation_token = nil.
Is that correct?
I am asking because there are a bunch of topics on the subject of allowing the user to request a second validation email in a separate controller action and in all of those topics the discussion is stuck on routing issues, because in the email that is sent, the :activation_token
is used as :id
and the error message comes up :id -> nil
Edit:
class AccountActivationsController < ApplicationController
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id])
user.activate
log_in user
flash[:success] = "Account activated!"
redirect_to user
else
flash[:danger] = "Invalid activation link"
redirect_to root_url
end
end
end
user.rb
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end