我正在尝试通过 Ruby on Rails 4.0 学习和使用我自己的用户模型和身份验证,但大多数教程(如果不是全部)似乎在最近的更新中已经过时了。它们中的任何一个都没有描述的方法有效。我绝对一无所知,这是我的用户模型:
class User
include Mongoid::Document
field :login, type: String
field :hash, type: String
field :salt, type: String
field :email, type: String
field :name, type: String
before_save :hash_password
validate :login, presence: true, uniqueness: true, length: { in: 4..24 }
validate :password, presence: true, confirmation: true, length: { in: 8..32 }
validate :email, presence: true
validate :name, presence: true
def hash_password
if password.present?
self.salt = BCrypt::Engine.generate_salt
self.hash = BCrypt::Engine.hash_secret(password, salt)
end
end
end
和控制器:
class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
redirect_to @user
else
render 'new'
end
end
def new
@user = User.new
end
private
def user_params
params.require(:user).permit(:login, :password, :password_confirmation, :email, :name)
end
end
然后,UnknownAttribute
当我保存用户时出现错误。我错过了什么?哪些字段有误?