【发布时间】:2015-09-11 04:58:31
【问题描述】:
我的代码:
class TestController < ApplicationController
def testpage
user = User.new(:email => 'test@sd.com', :encrypted_password => 'test')
user.save
end
end
我正在尝试从脚本内部手动保存用户,但它永远不会保存,但是我可以通过以下代码更新用户:
class TestController < ApplicationController
def testpage
user=User.find(2)
user.update(:email => 'new', :encrypted_password => 'new')
end
end
所以我的第二个代码工作正常,但不是第一个,我不知道为什么。
这是我的User 模型:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :manager
end
我不知道出了什么问题,我想拯救用户。我正在使用设计。 而且我也没有遇到任何错误。
编辑1: 在用 user.save 替换 user.save 时!我收到此错误。
验证失败:密码不能为空,密码确认 密码不匹配
【问题讨论】:
-
试试这个:
User.new(email: 'test@sd.com', password: 'test', password_confirmation: 'test')。这应该有效。 -
@Deep 没用,还有什么建议吗?
-
您遇到的错误是什么?只需将
user.save替换为user.save!,看看是什么错误。 -
验证失败:密码不能为空,密码确认与密码不匹配我有两个密码作为测试,但还是这个错误,奇怪...
-
试试这个
User.create!(email: 'test@sd.com', password: 'test', password_confirmation: 'test')
标签: ruby-on-rails devise