Rails - before_save不工作? [英] Rails -- before_save not working?

查看:433
本文介绍了Rails - before_save不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循Michael Hartl的RoR教程,它涵盖了密码加密的基本知识。这是目前用户模型:

I am following Michael Hartl's RoR tutorial, and it is covering the basics of password encryption. This is the User model as it currently stands:

class User < ActiveRecord::Base
    attr_accessor :password

    attr_accessible :name, :email,: password, :password_confirmation

    email_regex = /^[A-Za-z0-9._+-]+@[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+[A-Za-z]$/
                                              #tests for valid email addresses.

    validates :name, :presence => true,
                     :length => {:maximum => 50}
    validates :email, :presence => true,
                      :format => {:with => email_regex},
                      :uniqueness => {:case_sensitive => false}
    validates :password, :presence => true,
                         :length => {:maximum => 20, :minimum => 6},
                         :confirmation => true

    before_save :encrypt_password

    private

        def encrypt_password
            @encrypted_password = encrypt(password)
        end

        def encrypt(string)
            string
        end
end


$ b b

(显然这不是做任何加密,因为加密方法没有真正实现,但这不是我的问题)

(Obviously this isn't doing any encrypting because the encrypt method isn't really implemented but that's not my question)

然后我写了以下Spec(根据教程):

I then wrote the following Spec (according to the tutorial):

require 'spec_helper'

describe User do

    before(:each) do
        @attr = { :name => "Example User", :email => "user@example.com",
                  :password => "abc123", :password_confirmation => "abc123"}
    end

    describe "password encryption" do

        before(:each) do
            @user = User.create!(@attr) # we are going to need a valid user in order
                                        # for these tests to run.
        end

        it "should have an encrypted password attribute" do
            @user.should respond_to(:encrypted_password)
        end

        it "should set the encrypted password upon user creation" do
            @user.encrypted_password.should_not be_blank
        end

    end
end

第一个测试通过,但由于 @ user.encrypted_pa​​ssword 为nil,第二次测试失败。但我不明白为什么它是零,因为 encrypt_password 方法应该由 before_save 调用。

The first of these tests passes, but since @user.encrypted_password is nil, the second test fails. But I don't understand why it's nil since the encrypt_password method should be being called by before_save. I know I must be missing something -- can someone please explain?

推荐答案

encrypt_password方法不正确,应该读为:

The encrypt_password method is incorrect, it should read:

def encrypt_password
  self.encrypted_password = encrypt(password)
end

注意使用self,它将正确设置用户对象的属性,而不是创建一个被遗忘的实例变量。

Note the use of self, which will properly set the attribute for the user object rather than creating an instance variable which is forgotten.

这篇关于Rails - before_save不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆