我正在使用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:micropostsbefore_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 結束結束結束
你能指點一下'has_secure_password'嗎? – pduersteler