我試着尋找一個答案,但他們都使用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沒有解決我的問題 –