繁体   English   中英

为什么我的Ruby on Rails模型的属性没有设置为before_save?

[英]Why isn't my Ruby on Rails model's attribute being set with before_save?

我正在制作Ruby on Rails应用程序,无法设置模型的某些属性。 问题是我有一个:before_save方法,但是由于某种原因,encrypted_pa​​ssword和salt并没有保存到数据库中。 这是模型:

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :username, :email, :password

  before_save :encrypt_password

  ...

  private
      def encrypt_password
        logger.debug "Got to encrypt_password 1"
        logger.debug "encrypted_password is #{ @encrypted_password }"

        if @encrypted_password != nil and @encrypted_password != ""
          return # @encrypted_password was already set
        end

        logger.debug "Got to encrypt_password 2"
        @salt = make_salt if new_record?
        logger.debug "New salt = #{ @salt }"
        @encrypted_password = encrypt(@password)
        logger.debug "New encrypted_password = #{ @encrypted_password }"
        @password = "(encrypted)"
        logger.debug "Got to encrypt_password 3"
      end

在日志文件中,我看到以下内容:

    Started POST "/users" for 127.0.0.1 at Wed May 02 22:55:20 -0500 2012
    Processing by UsersController#create as HTML
      Parameters: {"commit"=>"Submit", "authenticity_token"=>"RY9fSMqb2+tdQ0fIjiEz8cfMTWTi012vCWdCvbxACLk=", "utf8"=>"\342\234\223", "user"=>{"username"=>"test6", "password"=>"[FILTERED]", "email"=>"test6@gmail.com"}}
      [1m[36m (0.1ms)[0m  [1mbegin transaction[0m
      [1m[35mUser Exists (0.2ms)[0m  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('test6@gmail.com') LIMIT 1
    Got to encrypt_password 1
    encrypted_password is 
    Got to encrypt_password 2
    New salt = 4f3464029393829aa562e533773f668c8471c51231611f6f214e654275f37184
    New encrypted_password = 0dafcff2fe75bb6f2b53afda79789cfe13bd3f733b817a0e2e30df98af5829bc
    Got to encrypt_password 3
      [1m[36mSQL (0.5ms)[0m  [1mINSERT INTO "users" ("created_at", "email", "encrypted_password", "is_done", "last_seen", "salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?)[0m  [["created_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["email", "test6@gmail.com"], ["encrypted_password", nil], ["is_done", false], ["last_seen", nil], ["salt", nil], ["updated_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["username", "test6"]]
      [1m[35m (0.7ms)[0m  commit transaction
    Redirected to http://localhost:3000/
    Completed 302 Found in 7ms (ActiveRecord: 1.4ms)

因此,它肯定是在制作盐和加密的密码。 但是数据库没有更新吗?

>> User.find(6)
=> #<User id: 6, username: "test6", email: "test6@gmail.com", encrypted_password: nil, is_done: false, salt: nil, last_seen: nil, created_at: "2012-05-03 03:55:20", updated_at: "2012-05-03 03:55:20">

尝试使用self.encrypted_password

ActiveRecord为您的属性创建getter / setter方法,即

def encrypted_password
    ...
    # return something from the db
end

def encrypted_password=(x)
    ...
    # set something in the db to x
end

而且,当您编写@encrypted_password ,实际上并没有使用这些方法,因此不会更新数据库。

您可以在日志中看到以下内容:

[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "users" ("created_at", "email", "encrypted_password", "is_done", "last_seen", "salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?)[0m [["created_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["email", "test6@gmail.com"], ["encrypted_password", nil], ["is_done", false], ["last_seen", nil], ["salt", nil], ["updated_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["username", "test6"]]

saltencrpyed_password设置为nil,因为没有更新属性,所以更新了类成员变量。

我认为您在def encrypt_password结尾时忘记了self.encrypted_password = @encrypted_password def encrypt_password

暂无
暂无

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

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