问题描述
我正在研究Michael Hartl的Learn Rails教程的修改版本.我在第6章,为用户建模.由于某些原因,我的用户无法在ActiveRecord上正确创建,也根本没有保存.
I'm working on a modified version of Michael Hartl's Learn Rails Tutorial. I'm on chapter 6, modeling users. For some reason, my users aren't creating correctly on ActiveRecord and aren't saving at all.
我将这些用户放置在我的 seeds.rb
文件中
I placed these users in my seeds.rb
file
user_1 = User.create(id: 1, name: 'Han Solo', email: '[email protected]')
user_2 = User.create(id: 2, name: 'Luke Skywalker', email: '[email protected]')
然后我运行 rails db:seed
,但是如果我转到 rails控制台
,它似乎没有创建任何用户:
Then I run rails db:seed
, but if I go to my rails console
, it appears like no users have been created:
Running via Spring preloader in process 24358
Loading development environment (Rails 5.0.0.1)
2.2.2 :001 > User.delete_all
SQL (1.1ms) DELETE FROM "users"
=> 0
2.2.2 :002 >
user.rb
用户模型
class User < ApplicationRecord
#Ensure Email Uniqueness by Downcasing the Email Attribute
before_save {self.email = email.downcase }
#validates name, presence, and length
validates :name, presence: true, length: { maximum: 100 }
#Validate presence, length, format, and uniqueness (ignoring case)
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: {maximum: 250}, format: {with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false}
#Adds ability to save securely hashed password_digest attribute to database
#Adds a pair of virtual attributes (password and password_confirmation)
#including presence validations upon object creation and a validation
#requiring that they match
#adds authenticate method that returns the user when the password is correct (and false otherwise)
has_secure_password
PASSWORD_FORMAT = /\A
(?=.{8,}) # Must contain 8 or more characters
(?=.*\d) # Must contain a digit
(?=.*[a-z]) # Must contain a lower case character
(?=.*[A-Z]) # Must contain an upper case character
(?=.*[[:^alnum:]]) # Must contain a symbol
/x
validates :password, presence: true, length: {minimum: 8}, format: {with: PASSWORD_FORMAT}
end
schema.rb
ActiveRecord::Schema.define(version: 20161020211218) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
end
end
任何人都知道发生了什么事吗?
Anyone have any idea what might be going on?
推荐答案
好,我知道了.
- 我从seeds.rb中取出了id.当我取出id属性时,它开始向我抛出用户没有有效密码的错误.然后,我添加了密码和密码确认,以符合我的用户模型中设置的密码标准.
- 更新了我的模型并添加了密码确认.
这是我更新的 user.rb
class User < ApplicationRecord
#Ensure Email Uniqueness by Downcasing the Email Attribute
before_save {self.email = email.downcase }
#validates name, presence, and length
validates :name, presence: true, length: { maximum: 100 }
#Validate presence, length, format, and uniqueness (ignoring case)
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: {maximum: 250}, format: {with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false}
#Adds ability to save securely hashed password_digest attribute to database
#Adds a pair of virtual attributes (password and password_confirmation)
#including presence validations upon object creation and a validation
#requiring that they match
#adds authenticate method that returns the user when the password is correct (and false otherwise)
has_secure_password
PASSWORD_FORMAT = /\A
(?=.{8,}) # Must contain 8 or more characters
(?=.*\d) # Must contain a digit
(?=.*[a-z]) # Must contain a lower case character
(?=.*[A-Z]) # Must contain an upper case character
(?=.*[[:^alnum:]]) # Must contain a symbol
/x
validates :password, presence: true, length: {minimum: 8}, format: {with: PASSWORD_FORMAT}
validates :password_confirmation, presence: true
end
和 seeds.rb
user_1 = User.create!(name: 'Han Solo', email: '[email protected]', password: 'Password123!', password_confirmation: 'Password123!')
user_2 = User.create!(name: 'Luke Skywalker', email: '[email protected]', password: 'Password123!', password_confirmation: 'Password123!')
我通过打开rails控制台,运行 User.delete_all
并看到他们清除了SQL记录来测试是否正确创建了用户.
I tested that the user is being created properly by opening up the rails console, running User.delete_all
, and seeing them wiped off the SQL record.
这篇关于Rails 5:ActiveRecord不创建记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!