3

我是 Rails 4 开发和 Rails 的新手。我红色的官方“Rails 入门”指南展示了如何创建博客。所以我想从0注册,auth系统做我自己的。

创建新用户时出现此错误。我不明白我做错了什么。有什么建议吗?

Git 仓库: https ://github.com/pumpurs/auth

用户控制器中的 ActiveRecord::UnknownAttributeError#create 未知属性:密码

class UsersController < ApplicationController 
   def new
     @user = User.new
   end
   def create 
     @user = User.new(params[:user].permit(:password, :email, :password_confirmation))
   end
private
   def user_params
     params.require(:user).permit(:password, :email, :password_confirmation)
   end
end

模型文件:

class User < ActiveRecord::Base
  before_save :encrypt_password
  validates_confirmation_of :password
  validates_presence_of :password, :on => :create 
  validates_presence_of :email
  validates_uniqueness_of :email

  def enrypt_password
if password.present?
  self.password_salt = BCript::Engine.generate_salt
  self.password_hash = BCript::Engine.generate.hash_seret(password, password_salt)
    end    
 end
end
4

1 回答 1

12

您需要添加attr_accessor :password到您的 User 模型中,以提供一个非 db-backed 属性,用于将您的 db-backed password_hash 属性作为基础。

于 2013-09-22T17:54:52.967 回答