我试着寻找一个答案,但他们都使用User.new等,而不是创建。Rails的用户模型没有被分配一个ID当使用User.create()
在具有有效属性的控制台中使用User.create()时,我得到一个用nil创建的用户ID和nil的时间戳。
这是我的用户模型。
class User < ApplicationRecord
attr_writer :name, :email
before_save {self.email = email.downcase}
validates :username, presence:true,
length: {maximum: 30},
uniqueness:true
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence:true,
length: {maximum:200},
format: {with: VALID_EMAIL_REGEX},
uniqueness: {case_sensitive: false}
has_secure_password
validates :password, presence: true,
length: {minimum:6}
end
在轨控制台我使用
User.create(username:"oiuedhioj", email:"[email protected]",password:"test",password_confirmation:"test")
并取回
<User id: nil, username: "Damhan", email: nil, created_at: nil, updated_at: nil, password_digest: "$2a$10$oGtRgcHigaHh/UCVX4QdM.AOgyGur8Oud5MyKZheUcQ...">
@Iceman你能解释一下你的评论,这样每个人都可以学习吗?操作系统是否也需要发布其控制器代码? –
@Iceman没有解决我的问题 –