2012-08-27 41 views
5

मैं Chapter 6 of railstutorial समाप्त कर दिया है, लेकिन मेरी सभी User model specs के बाद मैं निम्नलिखित त्रुटियों के साथ password & password_confirmation जोड़ा जल्द ही नाकाम रहने शुरू कर दिया है:railstutorial.org, अध्याय 6. अज्ञात विशेषता: पासवर्ड

Failures: 

    1) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    2) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    3) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    4) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    5) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    6) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    7) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    8) User when name is not present 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    9) User when email is not present 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    10) User when name is too long 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    11) User when email format is invalid should be invalid 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    12) User when email format is valid should be valid 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    13) User when email address is already taken 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    14) User when password is not present 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    15) User when password confirmation is nil 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    16) User when password doesn't match confirmation 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    17) User return value of authenticate method with valid password 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    18) User return value of authenticate method with invalid password 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    19) User return value of authenticate method with invalid password 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

    20) User with a password that's too short 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:17:in `new' 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

Finished in 7.43 seconds 
39 examples, 20 failures, 5 pending 

Failed examples: 

rspec ./spec/models/user_spec.rb:22 # User 
rspec ./spec/models/user_spec.rb:23 # User 
rspec ./spec/models/user_spec.rb:24 # User 
rspec ./spec/models/user_spec.rb:25 # User 
rspec ./spec/models/user_spec.rb:26 # User 
rspec ./spec/models/user_spec.rb:27 # User 
rspec ./spec/models/user_spec.rb:28 # User 
rspec ./spec/models/user_spec.rb:32 # User when name is not present 
rspec ./spec/models/user_spec.rb:37 # User when email is not present 
rspec ./spec/models/user_spec.rb:42 # User when name is too long 
rspec ./spec/models/user_spec.rb:46 # User when email format is invalid should be invalid 
rspec ./spec/models/user_spec.rb:57 # User when email format is valid should be valid 
rspec ./spec/models/user_spec.rb:73 # User when email address is already taken 
rspec ./spec/models/user_spec.rb:78 # User when password is not present 
rspec ./spec/models/user_spec.rb:83 # User when password confirmation is nil 
rspec ./spec/models/user_spec.rb:88 # User when password doesn't match confirmation 
rspec ./spec/models/user_spec.rb:96 # User return value of authenticate method with valid password 
rspec ./spec/models/user_spec.rb:102 # User return value of authenticate method with invalid password 
rspec ./spec/models/user_spec.rb:103 # User return value of authenticate method with invalid password 
rspec ./spec/models/user_spec.rb:109 # User with a password that's too short 
rake aborted! 

उपयोगकर्ता मॉडल पाया जा सकता है here अब, मैंने पहले ही स्रोत से मूल की तुलना की है लेकिन ऐसा लगता है कि मुझे कुछ याद आ रहा है। यह क्या है

किसी को भी मुझे बता सकते हैं क्यों इस त्रुटि आ रहा है नहीं है?

उत्तर

7

जोड़ें:

attr_accessor :password, :password_confirmation 

ट्यूटोरियल तो आप password_digest

+0

इसलिए मुझे 'attr_accessible' और 'attr_accessor' दोनों को जोड़ने की ज़रूरत है? लेकिन [नमूना स्रोत कोड] में (https://github.com/railstutorial/sample_app_2nd_ed/blob/master/spec/models/user_spec.rb), केवल 'attr_accesible' का उपयोग किया जाता है? – CuriousMind

+2

हां आपको दोनों को एक अलग उद्देश्य की सेवा करने की आवश्यकता है। यह ट्यूटोरियल – apneadiving

+0

कूल में अनुपलब्ध है, मदद के लिए धन्यवाद :) – CuriousMind

16

में एक डाइजेस्ट बचाने बस उपयोगकर्ता मॉडल फ़ाइल में has_secure_password जोड़ें यदि आपने यह अभी तक नहीं किया है कर देगा। password और password_confirmation -

class User < ActiveRecord::Base 
attr_accessible :email, :name, :password, :password_confirmation 
has_secure_password 
# .. 
end 
+1

यह बेहतर समाधान है, क्योंकि केवल 'attr_accessible' जोड़ने से एक और त्रुटि उत्पन्न होगी क्योंकि विधि' प्रमाणीकरण 'को अपरिभाषित किया जाएगा। यह दोनों त्रुटियों को हल करता है। – smessing

0

यह क्योंकि बहुत पहले before {} ब्लॉक दो विशेषताओं है कि उपयोगकर्ता मॉडल निर्दिष्ट नहीं करता जोड़ने की कोशिश कर रहा है इन त्रुटियों का निर्माण कर रहा है।

क्योंकि यह before {} ब्लॉक प्रत्येक परीक्षण (यह बिंदु है) से पहले चलाया जाता है, आपको हर परीक्षण पर एक त्रुटि मिल रही है और वे सभी असफल हो रहे हैं।

ActiveRecord::UnknownAttributeError: 
unknown attribute: password 

रूप codeneko के जवाब में कहते हैं, इस: यह है कि परीक्षण में विफल रहा है, दर असल, बल्कि ActiveRecord क्या उन विशेषताओं के साथ क्या करना नहीं जानता है और इससे पहले कि प्रत्येक परीक्षा वास्तव में भी चलाया जाता है एक त्रुटि का निर्माण नहीं है ट्यूटोरियल में आगे बढ़कर और उपयोगकर्ता मॉडल फ़ाइल में has_secure_password डालकर तय किया गया है। यह password और password confirmation विशेषताओं को स्वीकार करने के लिए ActiveRecord बताता है और सभी परीक्षण पास होंगे।

दुर्भाग्यवश ट्यूटोरियल आपको तुरंत has_secure_password पर टिप्पणी करने के लिए कहता है ताकि डाइविंग के उत्तर स्टैंड-इन के रूप में उपयोग करने से शायद एक बुरा विचार नहीं है जब तक कि ट्यूटोरियल में आप इसे असम्बद्ध नहीं करते।

1

मैं सिर्फ एक ही समस्या थी। मैं क्लाउड 9 पर ऐसा कर रहा था।

मैंने देखा है कि मेरी परीक्षण बिल्कुल नहीं या तो चल रहे थे।

ताज़ा मेरी फाइल पेड़ इन दोनों समस्याओं तय की। (सीधे मॉडल में password_confirmation या ट्यूटोरियल से किसी भी कोडिंग परिवर्तन करने

1

मैं कुछ घंटों के खर्च समाप्त हो गया और कोई जवाब नहीं मिला:

मैं लगाने की जरूरत नहीं: पासवर्ड या। । मेरे समस्या downcase लाइन पर किया जा रहा समाप्त हो गया:।

before_save { self.email = email.downcase } 

वह यह बताता है कि आप एक अलग तरह से जो मैंने किया था में इस लाइन कर सकते हैं नीचे देखें

https://www.railstutorial.org/book/_single-page#sec-creating_and_authenticating_a_user

लिस्टिंग 6 में।31, हम असाइनमेंट self.email = self.email.downcase (जहां स्वयं वर्तमान उपयोगकर्ता को संदर्भित करता है) के रूप में असाइनमेंट लिखा होगा, लेकिन उपयोगकर्ता मॉडल के अंदर स्वयं कीवर्ड दाईं ओर वैकल्पिक है: स्वयं। ईमेल = ईमेल.डाउनकेस

इस तरह से ऐसा न करें। यह मुझे त्रुटि संदेश हर बार दिया जब तक मैं before_save { self.email = email.downcase }

में लौट आया और हटाया: self.email = self.email.downcase

मैं एक noob तो पूरी तरह से यकीन नहीं क्यों यह फर्क पड़ा लेकिन यह किया हूँ।

0

मुझे लगता है कि उपयोगकर्ता मॉडल के लिए attr_accessor :password, :password_confirmation जोड़ने के लिए स्वीकृत उत्तर गलत है। क्योंकि अगर has_secure_password वस्तुतः उन गुणों को बनाता है, तो इसका मतलब है कि यह किसी कारण (सुरक्षा) के लिए करता है।

require 'bcrypt' 

    @user = User.new(name: 'test', password: BCrypt::Password.create("my password"), password_confirmation: 'my password') 

मैं इस पोस्ट पर इस बारे में लिखा था:

Rails - Unknown attribute password

कृपया मुझे सही कर अगर मैं गलत हूँ तो समाधान BCrypt आह्वान करने के लिए भी आप एक उपयोगकर्ता परीक्षण वस्तु बनाने के हो सकता है।