我正在使用http://ruby.railstutorial.org/chapters/user-microposts#code:sample_microposts教程构建Micropost模型。在运行测试以验证微博对象响应内容和user_id属性时,我收到此错误 未定义的方法'password ='代表#下面是“User.rb”&的代码块。 “Micropost spec.rb”。
**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&lt; ActiveRecord ::迁移def更改 create_table:用户做| t | t.string:name t.string:category
t.timestamps end end end
用户模型
class User&lt; ActiveRecord :: Base has_many:arts belongs_to:account
has_secure_password attr_accessible:name,:email 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:“关系”, 依赖:: destroy has_many:关注者,通过:: reverse_relationships,source :: follower
def generate_token(列) 开始 self [column] = SecureRandom.urlsafe_base64 User.exists结束?(column =&gt; self [column])
def following?(other_user) relationships.find_by_followed_id(other_user.id) end
def def!(other_user) relationships.create!(follow_id:other_user.id)结束 def unfollow!(other_user) relationships.find_by_followed_id(other_user.id).destroy end def accountName account.name 结束端
答案 0 :(得分:0)
只是猜测蓝色,但看着http://ruby.railstutorial.org/chapters/modeling-users#sec:adding_a_secure_password,似乎你错过了密码部分。
如果不是这种情况,那么您可能需要发布完整的模型(它看起来像是被剪掉了),也许是您的用户迁移文件。