2012-07-18 22 views
1

我正在使用amistad作爲朋友控制器。現在我想補充的朋友,但我得到的錯誤是:被調用的ID爲零,這將錯誤地爲4 - 如果你真的想要的ID爲零,請使用object_id .. amistad視圖?

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id.. 

這是我的鏈接添加好友:

<section> 
     <h1> 
     <%= @user.username %> 
     </h1> 

    <% unless current_user == @user %> 
<%= link_to "Arkadaşlarıma Ekle", friends_path(:friend_id => @user), :method => :post,class: "btn btn-large btn-primary" %> 
    <%end %> 
     </section> 

和我friendships_controller.rb:

class FriendshipsController < ApplicationController 

    before_filter :authenticate_user! 

    def index 
    @friends = current_user.friends 
    @pending_invited_by = current_user.pending_invited_by 
    @pending_invited = current_user.pending_invited 
    end 

    def new 
    @users = User.all :conditions => ["id != ?", current_user.id] 
    end 

    def create 
    invitee = User.find_by_id(params[:user_id]) 
    if current_user.invite invitee 
     redirect_to new_friend_path, :notice => "Successfully invited friend!" 
    else 
     redirect_to new_friend_path, :notice => "Sorry! You can't invite that user!" 
    end 
    end 

    def update 
    inviter = User.find_by_id(params[:id]) 
    if current_user.approve inviter 
     redirect_to new_friend_path, :notice => "Successfully confirmed friend!" 
    else 
     redirect_to new_friend_path, :notice => "Sorry! Could not confirm friend!" 
    end 
    end 

    def requests 
    @pending_requests = current_user.pending_invited_by 
    end 

    def invites 
    @pending_invites = current_user.pending_invited 
    end 

    def destroy 
    user = User.find_by_id(params[:id]) 
    if current_user.remove_friendship user 
     redirect_to friends_path, :notice => "Successfully removed friend!" 
    else 
     redirect_to friends_path, :notice => "Sorry, couldn't remove friend!" 
    end 
    end 

end 

怎麼能我爲amistad安排正確的觀點?有教程嗎?

回答

0

你的控制器有點麻煩,但我想這個錯誤是你發送一個參數「friend_id」,但在你的創建動作中,你試圖用一個名爲「user_id」的參數加載一個用戶。可能的方法:

current_user.invite invitee 

被調用與被邀請者爲零,所以你得到這個錯誤。

希望它有幫助! :)

相關問題