2012-04-29 15 views
6

यह अध्याय यह सुनिश्चित करने के लिए टोकन को याद रखने पर है कि उपयोगकर्ता साइनइन स्थिति याद रखी जाती है और सत्र केवल तभी साफ़ किया जाता है जब उपयोगकर्ता स्पष्ट रूप से संकेत देता है। मैं अपने ऐप में यह सुविधा रखने के महत्व को समझता हूं, इसलिए यह सुनिश्चित करना चाहता हूं कि यह सही तरीके से काम करता है। मैं त्रुटियों की एक गुच्छा हो रही है, हालांकि, जब मैंअध्याय 8 रेल ट्यूटोरियल टोकन त्रुटियों को याद रखें

$ bundle exec rspec spec/ 

चलाने के लिए और मुझे लगता है वे मेरे उपयोगकर्ता मॉडल के साथ क्या करना है, के रूप में वे सभी लेकिन एक होते हैं:

NoMethodError: 
     undefined method `remember_token=' for #<User:...> 

और पिछले शामिल

Failure/Error: it { should respond_to(:remember_token) } 

और फिर मेरी user_spec.rb, user.rb, और और authentication_pages_spec.rb फ़ाइलें, जो मैं से ज्यादातर (उचित भागों) यहाँ शामिल किया है को इंगित।

user.rb:

# == Schema Information 
# 
# Table name: users 
# 
# id   :integer   not null, primary key 
# name  :string(255) 
# email  :string(255) 
# created_at :datetime  not null 
# updated_at :datetime  not null 
# 

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation 
    has_secure_password 

    before_save { |user| user.email = email.downcase } 
    before_save :create_remember_token 

    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, 
        format:  { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
    validates :password, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 

    private 

     def create_remember_token 
     self.remember_token = SecureRandom.urlsafe_base64 
     end 
end 

authentication_pages_spec.rb:

require 'spec_helper' 

describe "Authentication" do 

    subject {page} 
    describe "signin page" do 
    before { visit signin_path } 
    it {should have_selector('h1', text: 'Sign in')} 
    it {should have_selector('title', text: 'Sign in')} 
    end 

    describe "signin" do 
    before {visit signin_path} 

    describe "with invalid information" do 
     before {click_button "Sign in"} 

     it {should have_selector('title', text: 'Sign in')} 
     it {should have_selector('div.alert.alert-error', text: 'Invalid')} 

     describe "after visiting another page" do 
       before { click_link "Home" } 
       it { should_not have_selector('div.alert.alert-error') } 
      end 
    end 

    describe "with valid information" do 
      let(:user) { FactoryGirl.create(:user) } 
      before do 
      fill_in "Email", with: user.email 
      fill_in "Password", with: user.password 
      click_button "Sign in" 
      end 

      it { should have_selector('title', text: user.name) } 
      it { should have_link('Profile', href: user_path(user)) } 
      it { should have_link('Sign out', href: signout_path) } 
      it { should_not have_link('Sign in', href: signin_path) } 
      end 
    end 
end 

और user_spec.rb की शुरुआत:

# == Schema Information 
# 
# Table name: users 
# 
# id   :integer   not null, primary key 
# name  :string(255) 
# email  :string(255) 
# created_at :datetime  not null 
# updated_at :datetime  not null 
# 

require 'spec_helper' 

describe User do 

    before do 
     @user = User.new(name: "Example User", email: "[email protected]", 
         password: "foobar", password_confirmation: "foobar") 
    end 

    subject { @user } 

    it { should respond_to(:name) } 
    it { should respond_to(:email) } 
    it { should be_valid } 
    it { should respond_to(:password_digest) } 
    it { should respond_to(:password) } 
    it { should respond_to(:password_confirmation) } 
    it { should respond_to(:authenticate) } 
    it { should respond_to(:remember_token) } 

    describe "remember token" do 
     before { @user.save } 
     its(:remember_token) { should_not be_blank } 
    end 
. 
. 
. 

किसी भी मदद की बहुत सराहना की जाएगी!

उत्तर

4

आप Heroku पर उत्पादन में इस त्रुटि मिलती है, के बाद आप चलाएँ:

heroku run rake db:migrate 

आप अपने अनुप्रयोग को पुनः आरंभ करने की जरूरत है:

heroku restart 
+0

यह मेरी समस्या को ठीक करता है क्योंकि यह केवल हीरोकू पर था। मैंने माइग्रेट करने से पहले साइन अप करने का प्रयास किया। उसके बाद, यहां तक ​​कि 'Heroku run db: माइग्रेट' भी समस्या को ठीक नहीं करेगा। हमेशा की तरह, मुझे इसे बार-बार चालू करने की कोशिश करनी चाहिए थी। – brodney

2

क्या आपने उपयोगकर्ता मॉडल में कॉलम जोड़ने के लिए माइग्रेशन उत्पन्न किया था?

$ rails generate migration add_remember_token_to_users 

इस संपादित नए क्षेत्र को जोड़ने के लिए माइग्रेशन फ़ाइल के बाद remember_token

और

कि आप मॉडल पर

$ bundle exec rake db:migrate 
$ bundle exec rake db:test:prepare 

आपका एनोटेशन करने के लिए नहीं दिखा जरूरत के बाद स्तंभ। सुनिश्चित करें कि आपने उपर्युक्त आदेश निष्पादित किए हैं।

+1

आपकी प्रतिक्रिया के लिए धन्यवाद! मैंने प्रवासन उत्पन्न किया था, लेकिन नया क्षेत्र नहीं जोड़ा था। हालांकि, जब मैंने कॉलम और इंडेक्स जोड़ा और डीबी कमांड चलाया, तो मुझे वही त्रुटियां मिलीं। इसलिए, मैंने _add_remember_token_to_users फ़ाइल को निर्देशिका में एक साधारण 'आरएम' द्वारा हटा दिया, फिर आदेशों को फिर से चलाया। मेरे फ़ाइल अब की तरह दिखता है {वर्ग AddRememberTokenToUsers skirmishdirmish

+0

कॉलम मौजूद है यदि कॉलम मौजूद है – rangalo

+0

यह करता है। क्या आपको लगता है कि इन सभी परिवर्तनों को वापस रोल करना और ताजा शुरू करना सबसे अच्छा होगा? यदि हां, तो क्या आप मुझे अन्य फ़ाइलों को नुकसान पहुंचाए बिना ऐसा करने का सबसे सुरक्षित तरीका बता सकते हैं? – skirmishdirmish

-1

मुझे अभी भी वही समस्या थी, अपरिभाषित विधि find_by_remember_token प्राप्त करना।

यहाँ क्या मैं इसे ठीक करने के लिए किया था है:

heroku run rake db:migrate 

फिर

git push heroku 
2

Heroku करने के लिए धक्का मुझे मिल समान परीक्षण त्रुटि असफल। मैंने स्ट्रिंग प्रकार के साथ remember_token कॉलम जेनरेट करने के लिए किया है।

rails generate migration add_remember_token_to_users remember_token:string --force 

rake db:migrate RAILS_ENV=test 

उसके बाद, परीक्षण पास।

+0

जब मुझे समस्या का सामना करना पड़ा तो यह मेरे लिए काम करता था। सवाल "क्यों," है, लेकिन मुझे लगता है कि जब मैं गहराई से गोताखोर करता हूं तो मुझे पता चल जाएगा। – MarsAtomic

0

मैंने मार्टल ट्यूटोरियल सीखते समय एक ही मुद्दे से मुलाकात की। मैंने इसे हल किया। जब आप डेटाबेस में पहले से ही कुछ उपयोगकर्ता डेटा रखते हैं, तो यह समस्या coz को दिखाती है, फिर "remember_token" माइग्रेट काम नहीं करेगा। आपको अपनी तिथि गंदगी को साफ करना होगा, फिर डीबी करें: माइग्रेट करें।इस तरह:

रेक db: ड्रॉप db: रेक डाटाबेस बनाने के लिए: विस्थापित (सावधान हो: यह आपके सभी डेटा मिट जाएगा)

आशा मदद

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