undefined method 'password=' for #<User:0xb88ac38>
I am building a Micropost Model using the http://ruby.railstutorial.org/chapters/user-microposts#code:sample_microposts tutorial. while running a test to verify that a micropost object responds to the content and user_id attributes i get this error undefined method 'password=' for # Below are the code blocks for "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*
require '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
Migration Users
class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name t.string :category
t.timestamps
end end end
User Model
class User < ActiveRecord::Base has_many :arts belongs_to :account
has_secure_password attr_accessible :name, :email 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: "Relationship", dependent: :destroy has_many :followers, through: :reverse_relationships, source: :follower
def generate_token(column) begin self[column] = SecureRandom.urlsafe_base64 end while User.exists?(column => self[column])
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 end end end
Just a guess into the blue, but looking at http://ruby.railstutorial.org/chapters/modeling-users#sec:adding_a_secure_password it seems like you're missing the passwords part.
If this is not the case, then you may need to post your complete model (it looks like it is cutted off) and maybe your migration file for users.
链接地址: http://www.djcxy.com/p/35954.html上一篇: 未知属性:构建时的id