在數據庫中創建新記錄時發生問題。
型號目前看起來是這樣的:Ruby on Rails。無法在數據庫中創建記錄
class User < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
before_save {!self.email.downcase}
validates :name, presence: true, length: {maximum: 50}
validates :email, presence: true, length: {maximum: 255}, format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
has_secure_password
validates :password, presence: true, length: {minimum: 6}
end
最初試圖創建的代碼記錄,但失敗,沒有任何錯誤。 作爲下一步,我嘗試在控制檯中手動創建一條記錄,但仍然有相同的結果。沒有錯誤可用。跳過驗證,正在重置數據庫,重新啓動服務器或排除從驗證沒有幫助電子郵件REGEXP(總是爲我煩惱的來源)...
user = User.new(name: "Fedor U", email: "[email protected]", password: "asdzxc123", password_confirmation: "asdzxc123")
=> #<User id: nil, name: "Fedor U", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$89SABxu2lOo3gj6DiqLZEOlSheAfEk0ex.5GYmJ5e8i...">
>> user.save
(0.2ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
(0.1ms) rollback transaction
=> false
>> user.errors.any?
=> false
user.errors.full_messages
=> []
>> user.save(validation: false)
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
(0.1ms) rollback transaction
=> false
任何想法,我究竟做錯了什麼? Rails 4.2.0,bcrypt 3.1.7。
目前數據庫爲空。
也許你的意思是'before_save {self.email.downcase! }'? – p4sh4
當然!非常感謝!我是一個真正的白癡:( –