简体   繁体   English

Rails控制台未将数据写入数据库

[英]Rails Console not writing data to database

I am working through the Hartl tutorial and I have run into a problem. 我正在阅读Hartl教程,但遇到了问题。 When I attempt to add a user in the console it doesn't return that it has saved any of the information I just entered. 当我尝试在控制台中添加用户时,它不会返回已保存我刚刚输入的任何信息的信息。 The code is below, hopefully you guys can steer me in the right direction. 代码在下面,希望你们可以引导我朝正确的方向发展。 The User model: 用户模型:

class User < ActiveRecord::Base

    attr_accessor :name, :email 

    before_save { self.email = email.downcase}
    validates :name, presence: true, length: {maximum: 50}
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, length: {maximum: 255},
                        format: { with: VALID_EMAIL_REGEX },
                        uniqueness: {case_sensitive: false}
    has_secure_password
    validates :password, length: { minimum: 6 }
end

the error: 错误:

2.2.0 :003 > User.create(name: "Jim Bob", email: "jim@bob.com", password: "jimmybob", password_confirmation: "jimmybob")
   (0.1ms)  begin transaction
  User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('jim@bob.com') LIMIT 1
  SQL (0.4ms)  INSERT INTO "users" ("password_digest", "created_at", "updated_at") VALUES (?, ?, ?)  [["password_digest", "$2a$10$I61hwA3iAQmzsT/wbb0wpOSannOBNPLtQsBhscjakS5OgCi6zJoMq"], ["created_at", "2015-02-13 23:45:32.636129"], ["updated_at", "2015-02-13 23:45:32.636129"]]
   (137.3ms)  commit transaction
 => #<User id: 1, name: nil, email: nil, created_at: "2015-02-13 23:45:32", updated_at: "2015-02-13 23:45:32", password_digest: "$2a$10$I61hwA3iAQmzsT/wbb0wpOSannOBNPLtQsBhscjakS5..."> 

I don't understand why the name and the email will not save. 我不明白为什么姓名和电子邮件将无法保存。 Please any help would be amazing. 请任何帮助将是惊人的。

Its because you have defined attr_accessor for name and email. 这是因为您已经为名称和电子邮件定义了attr_accessor。

attr_accessor will overwrite the methods generated by active records so its not been saved to database. attr_accessor将覆盖活动记录生成的方法,因此不会将其保存到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM