2011-06-13 27 views
8

मैं माइकल हार्टल के आरओआर ट्यूटोरियल का अनुसरण कर रहा हूं, और यह पासवर्ड एन्क्रिप्शन की मूल बातें को कवर कर रहा है। इस उपयोगकर्ता मॉडल है के रूप में यह वर्तमान में खड़ा है:रेल - पहले से काम नहीं कर रहे हैं?

class User < ActiveRecord::Base 
    attr_accessor :password 

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

    email_regex = /^[A-Za-z0-9._+-][email protected][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 

(जाहिर है यह किसी भी एनक्रिप्टिंग कर नहीं है, क्योंकि एन्क्रिप्ट विधि वास्तव में लागू नहीं है लेकिन यह है कि मेरे सवाल नहीं है)

मैं तो लिखा था निम्नलिखित युक्ति (ट्यूटोरियल के अनुसार):

require 'spec_helper' 

describe User do 

    before(:each) do 
     @attr = { :name => "Example User", :email => "[email protected]", 
        :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_password नहीं के बराबर है, दूसरे टेस्ट में विफल रहता है। लेकिन मुझे समझ में नहीं आता कि encrypt_password विधि before_save द्वारा विधि को क्यों कहा जाना चाहिए। मुझे पता है कि मुझे कुछ याद आना चाहिए - क्या कोई कृपया समझा सकता है?

उत्तर

20

encrypt_password विधि गलत है, यह पढ़ना चाहिए:

def encrypt_password 
    self.encrypted_password = encrypt(password) 
end 

नोट स्वयं के उपयोग, जो ठीक से एक उदाहरण चर जो भुला दिया जाता है बनाने की बजाय उपयोगकर्ता वस्तु के लिए विशेषता निर्धारित होगा।

+1

मैं तुम्हें "स्वीकार किए जाते हैं जवाब" दिया क्योंकि यह मेरी समस्या हल हो गई है, लेकिन मैं अभी भी यह इतना के साथ वैचारिक मुद्दों हो रहा है अगर आप उन को हल करना चाहते हैं मेरे अनुवर्ती सवाल यहाँ है: http://stackoverflow.com/questions/6327174/rails-self-vs :) अंक अर्जित करने का एक और अवसर। – Kvass

0

यह एक पुराना सवाल है और यह एक टिप्पणी है लेकिन मेरे पास अभी तक टिप्पणी करने के लिए पर्याप्त प्रतिष्ठा नहीं है। बस इस प्रश्न को भी लिंक करना चाहता था क्योंकि यह self के बारे में कुछ ठोस विवरण में जाता है।

Why isn't self always needed in ruby/rails/activerecord?

संबंधित मुद्दे