我正在阅读迈克尔·哈特尔的Rails书籍,我正处于第10章的一半 - 从事帐户激活工作。
我让所有内容都与邮件程序有关,但当我尝试添加新用户时,我收到以下错误消息:“undefined method`activation_digest ='for#”
我一直试图在书中尽可能地遵循。我在这里有users_controller.rb:
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]
def new
@user = User.new
end
def index
@users = User.paginate(page: params[:page], :per_page => 10)
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
else
render 'edit'
end
end
def edit
@user = User.find(params[:id])
end
#confirms if a user is logged in
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please Log In."
redirect_to login_url
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
这是我的Model / user.rb:
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token
before_save :downcase_email
before_create :create_activation_digest
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\-.]+\.[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 }
# Returns the hash digest of the given string.
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
#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
#forgets a user
def forget
update_attribute(:remember_digest, nil)
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
我有这条路线:
root 'static_pages#home'
get 'sessions/new'
get 'users/new'
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]
如果需要了解更多信息,请与我们联系。我的名字是sample_app,我的应用程序在Github上有我的应用程序,我的用户名是ravenusmc。
答案 0 :(得分:1)
在Github上查看您的项目,您的User
模型没有activation_token
或activation_digest
列,模型也没有将它们定义为属性。
您的User
模型正试图写入User#create_activation_digest
函数中的这些列,这很可能会导致问题。
您需要编写迁移以将这些列添加到您的用户模型,或者添加属性(即attr_accessor
),如果它们不是要保留的话。