2012-05-06 49 views
0

我正在使用http://ruby.railstutorial.org/chapters/user-microposts#code:sample_microposts教程構建Micropost模型。當運行一個測試來驗證micropost對象響應內容和user_id屬性時,我得到這個錯誤 未定義的方法'password ='for#以下是「User.rb」&「Micropost spec.rb」的代碼塊。未定義的方法'密碼='爲#<用戶:0xb88ac38>

**User.rb** 

    class User < ActiveRecord::Base 
    has_many :arts 
    belongs_to :account 
    attr_accessible :name, :email,:password, :password_confirmation 

     has_many :microposts, dependent: :destroy 
    has_many :relationships, foreign_key: "follower_id", dependent: :destroy 
     has_many :followed_users, through: :relationships, source: :followed 
    has_many :reverse_relationships, foreign_key: "followed_id", 
            class_name: "Relationship", 
            dependent: :destroy 
     has_many :followers, through: :reverse_relationships, source: :follower 

    def following?(other_user) 
    relationships.find_by_followed_id(other_user.id) 
    end 

     def follow!(other_user) 
    relationships.create!(followed_id: other_user.id) 
    end 

    def unfollow!(other_user) 
     relationships.find_by_followed_id(other_user.id).destroy 
    end 
     def accountName 
     account.name 

* Micropost_spec *

需要 'spec_helper'

describe Micropost do 

    let(:user) { FactoryGirl.create(:user) } 
    before do 
    # This code is wrong! 
    @micropost = Micropost.new(content: "Lorem ipsum", user_id: user.id) 
    end 

    subject { @micropost } 

    it { should respond_to(:content) } 
    it { should respond_to(:user) } 

end 

遷移用戶

類調​​用createUsers <的ActiveRecord ::遷移DEF改變 create_table:用戶do | t | t.string:命名 t.string:類

t.timestamps 
end end end 

用戶模型

類用戶<的ActiveRecord :: Base的的has_many:藝術belongs_to的:帳戶
has_secure_password attr_accessible:姓名,電子郵件地址: has_many:microposts

before_create { generate_token(:auth_token)} 
    has_many :microposts, dependent: :destroy has_many :relationships, foreign_key: "follower_id", dependent: :destroy 
    has_many :followed_users, through: :relationships, source: :followed has_many :reverse_relationships, foreign_key: 

「followed_id」, CLASS_NAME: 「關係」, 依賴性:破壞 的has_many:追隨者,通過:reverse_relationships,來源::跟隨

DEF generate_token(列) 開始 自[柱] = SecureRandom.urlsafe_base64 端而User.exists?(柱=>自[柱])

def following?(other_user) 
relationships.find_by_followed_id(other_user.id) end 

DEF遵循!(other_user) relationships.create(followed_id:other_user.id)!結束 高清取消關注(other_user) relationships.find_by_followed_id(other_user.id).destroy結束DEF帳戶名account.name 結束結束結束

+0

你能指點一下'has_secure_password'嗎? – pduersteler

回答

0

只是一個猜測!進入藍色,但看着http://ruby.railstutorial.org/chapters/modeling-users#sec:adding_a_secure_password它似乎是你缺少密碼部分。

如果情況並非如此,那麼您可能需要發佈您的完整模型(它看起來像是被切斷),也可能是您的用戶遷移文件。

+0

感謝您的回覆,我已經將我的遷移文件放在上面的用戶中,當你說模型時,你的意思是,微博還是用戶模型? – Jide

+0

用戶模式,因爲您的用戶需要使用密碼進行身份驗證 – pduersteler

+0

我在上面插入了我的用戶模型 – Jide

相關問題