被称为nil的id,这将错误地为4 - 如果你真的想要id为nil,请使用object_id .. amistad views?

时间:2012-07-18 10:45:15

标签: ruby-on-rails model-view-controller

我使用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安排正确的观点?有教程吗?

1 个答案:

答案 0 :(得分:0)

您的控制器有点乱,但我认为错误是您发送了一个参数“friend_id”,但在您的创建操作中,您正在尝试使用名为“user_id”的参数加载用户。可能是方法:

current_user.invite invitee
正在调用

,被邀请者为零,因此您收到此错误。

希望它有所帮助! :)