2012-02-14 61 views
2

的問題,這是一個錯誤,我得到扶手:經與後續跟隨系統

undefined method `followed_users?' for #<User:0x007fdadbf11e28> 

提取的源(左右線#3):

1: <% unless current_user?(@user) %> 
2: <div id="follow_form"> 
3: <% if current_user.followed_users?(@user) %> 
4:  <%= render 'unfollow' %> 
5: <% else %> 
6:  <%= render 'follow' %> 

如果我刪除了問號,我得到這個錯誤

undefined method `model_name' for NilClass:Class 

提取的源(圍繞線#1):

1: <%= form_for current_user.relationships.find_by_followed_id(@user), 
2:    html: { method: :delete }, 
3:    remote: true do |f| %> 
4: <div class="actions"><%= f.submit "Unfollow" %></div> 

我感到非常困惑這裏是我的

用戶模型

has_many :microposts 
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 

關係模型

attr_accessible :followed_id 

belongs_to :follower, class_name: "User" 
belongs_to :followed, class_name: "User" 

validates :follower_id, presence: true 
validates :followed_id, presence: true 

用戶控制器

def show 
    @user = User.find(params[:id]) 
    @micropost = Micropost.new 
    @microposts = @user.microposts.paginate(page: params[:page]) 
    end 

關係控制器

def create 
    @user = User.find(params[:relationship][:followed_id]) 
    current_user.follow!(@user) 
    respond_to do |format| 
     format.html { redirect_to @user } 
     format.js 
    end 
    end 

    def destroy 
    @user = Relationship.find(params[:id]).followed 
    current_user.unfollow!(@user) 
    respond_to do |format| 
     format.html { redirect_to @user } 
     format.js 
    end 
    end 

回答

3

你不應該僅僅是在第3行

+0

哇謝謝你,我是學出軌教程使用if current_user.following?(@user),而不是if current_user.followed_users?(@user)並且它編碼那樣!感謝您指出了這一點 – Kellogs 2012-02-14 03:49:04

+0

乾杯,我剛剛發給邁克爾·哈特爾一封電子郵件,讓他來解決它。很好的接收。 – 2012-02-14 03:57:59

+0

感謝擡頭。這是一個過分熱心的搜索和替換的結果。它是固定的。 – mhartl 2012-02-14 05:50:49