Ruby on rails Rails教程用户.save

Ruby on rails Rails教程用户.save,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在编写迈克尔·哈特尔的教程(第6章)。当我尝试在rails控制台中创建新用户时: user = User.new(name: "Lord of Darkness", email: "LoD@hell.com") 我得到: => #<User id: nil, name: "Lord of Darkness", email: "LoD@hell.com", created_at: nil, updated_at: nil, password_digest: nil>

我正在编写迈克尔·哈特尔的教程(第6章)。当我尝试在rails控制台中创建新用户时:

user = User.new(name: "Lord of Darkness", email: "LoD@hell.com")
我得到:

 => #<User id: nil, name: "Lord of Darkness", email: "LoD@hell.com",
 created_at: nil, updated_at: nil, password_digest: nil>
这绝对不是我想要的。我的用户规格通过了所有测试。 My user.rb看起来像这样:

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }

  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
             uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end
class用户

是什么导致此保存问题?如果不解决此错误,我将无法继续学习教程。

我相信您的错误是由于您创建的
用户没有密码


您的
用户
模型验证密码并要求其存在,但当您从命令行创建新的
用户
时,他/她还没有密码。

签出用户。有效吗?或者它似乎是电子邮件id存在,签出user.errors.messages或full_messages,我认为它是user.create()而不是user.save()检查验证错误,正如@Amar所建议的,你应该能够跟踪它。你是对的。问题在于,在用户定义中,我忘记输入密码和密码确认。非常感谢你!
class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }

  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
             uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end