我正在开发一个拥有用户模型的网络应用。您可以注册或试用该应用并成为“来宾”。在添加guest选项之前,我的注册功能运行良好。我基于Railscast来做这件事(http://railscasts.com/episodes/393-guest-user-record),当我遇到麻烦时
以下是我的用户模型代码
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_many :bookings
has_one :account, dependent: :destroy
# before_save :downcase_email, allow_nil: true
before_save :create_remember_token
validates_presence_of :name, :email, :password, :password_confirmation, unless: :guest?
validates_uniqueness_of :email, case_sensitive: false , allow_blank: true
validates :name, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, format: { with: VALID_EMAIL_REGEX }, unless: :guest?
validates :password, length: { minimum: 6 }, unless: :guest?
def downcase_email
{ |user| user.email = email.downcase }
end
# has_secure_password
# override has_secure_password to customize validation until Rails 4.
require 'bcrypt'
attr_reader :password, :password_confirmation
include ActiveModel::SecurePassword::InstanceMethodsOnActivation
##def to_param
## "#{id}.#{name.parameterize}"
##end
def self.new_guest
new { |u| u.guest = true }
end
def name
guest ? "Guest" : name
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
这是我的用户控制器代码:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update]
before_filter :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = params[:user] ? User.new(params[:user]) : User.new_guest
if @user.save
sign_in @user
@user.create_account.accountPlan = "Free"
##flash[:success] = "Welcome to the HighTide!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
#flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
private
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
end
您可能会注意到某些代码已被注释掉。我没有能够在没有电子邮件的情况下在数据库中存储(并且为了快速和肮脏的事情,我现在正在评论回调before_save,我也可以补充一点,我使用Michael Hartl tut的指南)
当我运行此代码时,我会得到一个unknown attribute: password_confirmation error
,但当我发表评论时
require 'bcrypt'
attr_reader :password, :password_confirmation
include ActiveModel::SecurePassword::InstanceMethodsOnActivation
并使用has_secure_password instead
我的堆栈级别太深
我还没想出要做什么