为什么 user.save 为 true 但 email 显示为 nil?

发布于 2024-12-02 10:30:47 字数 1924 浏览 0 评论 0原文

我正在使用嵌套模型表单进行注册,并作为初学者解决这些问题。尽管我没有真正理解,但特别出现的一个问题是 user.email 返回为 nil

在开始使用嵌套模型表单之前,我可以毫无问题地在控制台中创建记录。但现在我无法创建记录,并且创建的一些最新记录的电子邮件地址为 nil。 (我不确定它是否与嵌套模型有任何关系,但这是我开始变得混乱的参考点。)

如果我进入rails控制台创建一个新用户/Profile,我遵循这个过程:

user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.first_name = ""
...
user.profile.save
user.save

一切都很顺利,直到 user.save,这给了我 NameError: undefined local variable or method 'params' for #。在 rails console 中,它精确指向 create_profile 中的 user.rb:25

所以这是我的 User 模型:

class User < ActiveRecord::Base
  attr_accessor :password, :email
  has_one :profile, :dependent => :destroy
  accepts_nested_attributes_for :profile

  validates :email, :uniqueness => true,
            :length => { :within => 5..50 },
            :format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }
  validates :password, :confirmation => true,
            :length => { :within 4..20 },
            :presence => true,
            :if => :password_required?

  before_save :encrypt_new_password
  after_save :create_profile

  def self.authenticate(email, password)
    user = find_by_email(email)
    return user if user && user.authenticated?(password)
  end

  def authenticated?(password)
    self.hashed_password == encrypt(password
  end

  protected
    def encrypt_new_password
      return if password.blank?
      self.hashed_password = encrypt(password)
    end
    def password_required?
      hashed_password.blank? || password.present?
    end
    def encrypt(string)
      Digest::SHA1.hexdigest(string)
    end
  end

任何人都可以帮我弄清楚发生了什么事吗?

更新:我尝试更改我的正则表达式,但我仍然看到电子邮件为零。虽然之前的 SO 帖子说不要在没有测试的情况下盲目复制正则表达式,所以也许我只是没有正确测试它。不过好消息是:我不再收到错误消息。

I'm using a nested model form for sign-up and am working through the kinks as a beginner. One issue that popped up in particular though that I don't really get is user.email is returning as nil.

Before I started playing around with the nested model form, I could create records in the console wihtout a problem. Now, however I can't create records and some of the latest records created have nil as their email. (I'm not sure if it has anything to do with the nested model at all, but that's my reference point for when it started going haywire.)

If I go into rails console to create a new User/Profile, I follow this process:

user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.first_name = ""
...
user.profile.save
user.save

Everything goes well until user.save, which gives me the NameError: undefined local variable or method 'params' for #<User:>. In rails console it pinpoints to user.rb:25 in create_profile

So here is my User model:

class User < ActiveRecord::Base
  attr_accessor :password, :email
  has_one :profile, :dependent => :destroy
  accepts_nested_attributes_for :profile

  validates :email, :uniqueness => true,
            :length => { :within => 5..50 },
            :format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }
  validates :password, :confirmation => true,
            :length => { :within 4..20 },
            :presence => true,
            :if => :password_required?

  before_save :encrypt_new_password
  after_save :create_profile

  def self.authenticate(email, password)
    user = find_by_email(email)
    return user if user && user.authenticated?(password)
  end

  def authenticated?(password)
    self.hashed_password == encrypt(password
  end

  protected
    def encrypt_new_password
      return if password.blank?
      self.hashed_password = encrypt(password)
    end
    def password_required?
      hashed_password.blank? || password.present?
    end
    def encrypt(string)
      Digest::SHA1.hexdigest(string)
    end
  end

Can anyone help me figure out what's going on?

UPDATE: I tried changing my regex but I'm still seeing nil for email. Though a prior SO post said not to blindly copy regex without testing, so maybe I just didn't test it correctly. Good news though: I no longer get the error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2024-12-09 10:30:47

attr_accessor 只是定义对象的“属性”,与 ActiveRecord 模型的 attributes 无关(attributes 是一个 Hash 从表中获取的字段和值)。

ActiveRecord 不会保存 attr_accessor 定义的此类“属性”。 (本质上,attr_accessor同时定义了attr_reader和attr_writer(即“getter”和“setter”))

attr_accessor simply defines a "property" on the object and has no relation to the attributes of a ActiveRecord model (attributes is a Hash of the fields and values obtained from a table).

ActiveRecord does not save such "properties" as defined by the attr_accessor. (Essentially, attr_accessor defines a attr_reader and attr_writer (i.e. "getter" and "setter") at the same time)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文