2012-03-30 36 views
0

我已經爲用戶設置了自我引用關係模型,以便彼此成爲朋友。我可以創建一個朋友請求(我的數據庫中有兩個條目,一個是友誼,一個是反向友誼),沒有任何問題。嘗試摧毀/拒絕友誼關係時未定義的方法「破壞」

雖然我拒絕拒絕朋友請求。我不斷收到這個錯誤,當我按下下降這需要我去摧毀行動:

NoMethodError in FriendshipsController#destroy 
undefined method `destroy' for nil:NilClass 

之一數據庫記錄,待友誼,即使我得到上述錯誤被破壞,如果我註釋掉@friendship2 = @user.friendships.find_by_friend_id(params[:id]).destroy,另一個記錄也被破壞。所以它看起來像兩條線都單獨工作,但返回上述錯誤。

解決方案:它看起來像我的兩個銷燬語句引用相同的記錄。我在控制器中註釋了錯誤的代碼,並添加了適用於我的代碼。

這是我的友誼控制器:

class FriendshipsController < ApplicationController 
    before_filter :authenticate, :only => [:update, :create, :destroy] 

def create 
    @user = User.find(current_user) 
    @friend = User.find(params[:friend_id]) 
    params[:friendship1] = {:user_id => @user.id, :friend_id => @friend.id, :status => 'requested'} 
    params[:friendship2] = {:user_id => @friend.id, :friend_id => @user.id, :status => 'pending'} 
    @friendship1 = Friendship.create(params[:friendship1]) 
    @friendship2 = Friendship.create(params[:friendship2]) 
    redirect_to @friend 
end 
end 

    def destroy 
    @user = User.find(params[:user_id]) 
    @friend = User.find(params[:id]) 
    #@friendship2 = @user.friendships.find_by_friend_id(params[:id]).destroy 
    #@friendship1 = @friend.friendships.find_by_id(params[:user_id]).destroy 
    @friendship1 = @user.friendships.find_by_friend_id(@friend.id).destroy #removes the requested friendship 
    @friendship2 = @friend.friendships.find_by_friend_id(@user.id).destroy #removes the pending friendship 

    flash[:success] = "Removed." 
    redirect_to @user 
    end 
end 

這是我的用戶模型:

class User < ActiveRecord::Base 
    has_many :friendships, :dependent => :destroy 

    has_many :friends, 
    :through => :friendships, 
    :conditions => "status = 'accepted'", 
    :source => :friend 

    has_many :pending_friends, 
    :through => :friendships, 
    :conditions => "status = 'pending'", 
    :foreign_key => "user_id", 
    :source => :friend 

    has_many :requested_friends, 
    :through => :friendships, 
    :source => :friend, 
    :conditions => "status = 'requested'"  
end 

這是我的視圖(用戶/ show.html.erb):

<% if signed_in? && @user == current_user %> 
    <% unless current_user.pending_friends.empty? %> 
    <h2>Pending</h2> 
    <% current_user.pending_friends.each do |pending| %> 
     <%= pending.name %> 
     <%= link_to '[Accept]', friendship_path(:user_id => current_user, :id => pending), :method => :put, :confirm => "Accept?" %> 
     <%= link_to '[Decline]', friendship_path(:user_id => current_user, :id => pending), :method => :delete, :confirm => "Decline?" %> 
    <% end %> 
    <% end %> 
<% end %> 

我有兩個問題。

  1. 我從一些教程/討論有關the use of inverse_friendships看到的,但是這真的有必要嗎?到目前爲止,我還沒有看到這個需求,但是再一次,我只是編寫了我的創建和銷燬行動。

  2. 未定義的方法'destroy'錯誤是怎麼回事?

感謝您瀏覽這段文字! :D

回答

1

關於第一個,取決於你和你的系統。創建不同的變量來保存每種類型的友誼雖然可能有點頑固,但它給你提供了完整的信息,更多的控制,並且數據庫中沒有數據重複。

關於第二個,你應該檢查你的查詢是否返回結果。由於您的例外:

未定義的方法'破壞」的零:NilClass

說,有一個在NilClass沒有方法 「破壞」(沒有朋友)。只需檢查:id參數以及find_by_friend_id是否返回任何內容。

+0

謝謝你澄清這兩個問題。我仍然無法找到錯誤,並且也包含了我的觀點。我認爲問題出現在@ @ friendship1 = @ friend.friendships.find_by_id(params [:user_id])。destroy'行中。請原諒我的經驗不足(Rails仍然是新手)。我會繼續研究這一點。 – Huy 2012-03-30 06:13:31

+0

謝謝你的幫助。這最終導致我發現答案。 – Huy 2012-03-30 08:43:22

+0

不客氣:) – 2012-03-30 19:10:51

1

在去壞之前進行檢查。

friendship = @friend.friendships.find_by_id(params[:user_id]) 
@friendship1 = friendship.destroy if !friendship.nil? 

,還可以使用可以使這個destory成具有callbacks幫助模型。

+0

這幫助我更接近解決方案。雖然這刪除了錯誤消息,但它仍然沒有刪除requested_friendship。我能夠弄清楚,我的兩個破壞是引用相同的記錄。現在我只需要弄清楚如何通過user_id來傳遞friend_id ... – Huy 2012-03-30 08:33:19

+0

謝謝你的幫助,Vik。我能弄明白。給德米特里勾上了對號,但也讚揚了你。 – Huy 2012-03-30 08:44:06