1

我正在阅读 Michael Hartl 的 Rails 教程,但在第 10 章我无法理解这一行 authenticated?(:activation, params[:id])

根据作者的说法,这一行用于比较activation_digesttoken这意味着令牌将在以下位置可用params[:id]

这就是我感到困惑的地方。是否params[:id]检索用户的ID他们如何将 ID 与activation_digest 进行比较?

但是authenticated?(:remember, cookies[:remember_token])对我来说很有意义。任何人 ?您的帮助将不胜感激!

相关代码如下所示:

account_activations_controller.rb

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

路线.rb

Rails.application.routes.draw do
  get 'password_resets/new'

  get 'password_resets/edit'

  get 'sessions/new'

  get 'users/new'

  root             'static_pages#home'
  get 'help'    => 'static_pages#help'
  get 'about'   => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup'  => 'users#new'
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  delete 'logout'  => 'sessions#destroy'
  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]

用户.rb

class User < ActiveRecord::Base
    attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
    before_create :create_activation_digest
    validates :name,   presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
    validates :email,  presence: true, length: { maximum: 255 },
                       format: { with: VALID_EMAIL_REGEX },
                       uniqueness: { case_sensitive: false }
    has_secure_password
    validates :password, length: { minimum:6 }, allow_blank: true

class << self
    # Returns the hash digest of the given string.
  def 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 new_token
    SecureRandom.urlsafe_base64
  end
end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
    return false if remember_digest.nil?
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end

  # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

  # Activates an account.
  def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

  # Sets the password reset attributes.
  def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

private
# Converts email to all lower-case.
def downcase_email
  self.email = email.downcase
end

# Creates and assigns the activation token and digest.
def create_activation_digest
  self.activation_token  = User.new_token
  self.activation_digest = User.digest(activation_token)
 end
end
4

1 回答 1

1

我将它发布在 ruby​​ on rails talk on google 上,终于有人回答了我的问题。对于那些正在阅读 Michael Hartl 的书的人,您可能会发现这很有用。

无论如何,这点params[:id] 与routes.rb文件有关,这表明它与路由生成的url有关。在这种情况下 。我在 Rails 中使用 RESTful url

假设 URL 是: https ://www.examples.com/account_activations/token_fFb_F94mgQtmlSvRFGsITw/edit?email=michael%40michaelhartl.com

那么 params[:id] 将是“token_fFb_F94mgQtmlSvRFGsITw”

更好的解释可以在这里找到点击这里查看

我的问题是我将params[:id]中的“:id”误认为是用户表中的数字 id 属性。希望你不会犯和我一样的错误。祝你好运 !

于 2015-04-10T02:27:33.877 回答