저는 Michael Hartl의 Learn Rails 튜토리얼을 수정했습니다. 저는 6 장의 모델링 사용자입니다. 어떤 이유로 ActiveRecord에서 사용자가 올바르게 작성되지 않아 저장하지 않습니다. 레일 5 : ActiveRecord가 레코드를 생성하지 않습니다.
나는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
를 실행 내 seeds.rb
파일에서 이러한 사용자를 배치,하지만 난 내 rails console
에 가면, 어떤 사용자가 생성되지 않았습니다처럼 나타납니다
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+\-.][email protected][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
아무도 무슨 일이 벌어지고 있는지 알 수 없습니까?
데이터베이스에 이미 해당 사용자가 있습니까? 'valid? '를 체크했을 때 나타납니다. 전자 메일 주소가 이미 사용 되었기 때문에 false를 반환합니다. – rdubya
예를 들어 실제 Gmail 주소 대신 IANA 샌드 박스 도메인'@ example.com'을 사용하십시오. 주소를 소유 한 사람이 스팸봇에 의해 수확되는 것을 막아줍니다. – max
을 호출 한 후'user.valid?'를 호출하면'user.errors.full_messages'를 호출하여 같은 것을 방지 할 수 있습니다. –