1

所以我试图从 Amistad 为我的友谊模型设置一个视图,但我不断收到这个错误:“undefined method `each' for nil:NilClass”

这是我的友谊.rb 模型

class Friendship < ActiveRecord::Base
  include Amistad::FriendshipModel

    attr_accessible :user_id, :friend_id
end

这是我的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 create
    @Friend = User.find(params[:user_id])
    @friendship_created = current_user.invite(@Friend)
  end

  def approve
    @Friend = User.find(params[:user_id])
    @friendship_approved = current_user.approve(@Friend)
    @friends = current_user.friends
    @pending_invited_by = current_user.pending_invited_by
  end

  def remove
    @Friend = User.find(params[:user_id])
    @friendship = current_user.send(:find_any_friendship_with, @Friend)
    if @friendship
      @friendship.delete
      @removed = true
    end
   end
  end

我的用户模型 user.rb

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
  has_many :cereals, dependent: :destroy

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]
  include Amistad::FriendModel

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :avatar, :avatar_cache, :remove_avatar, :firstname, :lastname, :phone, :urlname
  # attr_accessible :title, :body

  attr_accessor :login

  def self.find_first_by_auth_conditions(warden_conditions)
      conditions = warden_conditions.dup
      if login = conditions.delete(:login)
        where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
      else
        where(conditions).first
      end
    end

            def update_with_password(params={})
        current_password = params.delete(:current_password)

        if params[:password].blank?
          params.delete(:password)
          params.delete(:password_confirmation) if params[:password_confirmation].blank?
        end

                result = if params[:password].blank? || valid_password?(current_password) 
          update_attributes(params)
        else
          self.attributes = params
          self.valid?
          self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
          false
        end

        clean_up_passwords
        result
      end
    end

和 users_controller.rb

class UsersController < ApplicationController
    def show
      if params[:id].nil? && current_user
        @user = current_user
      else
        @user = User.find(params[:id])
      end
      @cereal = current_user.cereals.build if signed_in?
      @cereals = @user.cereals.paginate(page: params[:page])
  end

  def first_time
      if params[:id].nil? && current_user
        @user = current_user
      else
        @user = User.find(params[:id])
      end
  end

    def edit
        if params[:id].nil? && current_user
            @user = current_user
        else
            @user = User.find(params[:id])
        end
  end

    def profile
        @username = params[:id]
        @title = "User Profile for #{@username}"
        @user = User.find_by_username(@username)
        @users = User.all :conditions => ["id != ?", current_user.id]
  end 

end

有人知道发生了什么吗?我还是 Rails 的新手,可以使用一些见解。

风景:

.row
    #dashboard.span12
        = @user.username
        #profile_pic=link_to image_tag(@user.avatar, border: '0'), action: 'profile', controller: 'users'
    #bowl_outline.span9
        Bowl

    #friends.span3
        Friends
        %ul
        - for @user in @friends
            %li
                - if current_user.friend_with? user
                    = user.username
                    |
                    You are already friends!
                - elsif current_user.invited? user
                    = user.username
                    | 
                    Pending request ...
                - elsif user.invited? current_user
                    = user.username
                    |
                    = link_to "Confirm friend?", friend_path(user), :method => "put"
                - else
                    = user.username
                    = link_to "Add friend?", friends_path(:user_id => @user), :method => "post"
4

2 回答 2

1

你不应该有一个实例变量作为你的 for 循环变量。

for user in @friends

我相信@friends = current_user.friends返回一个数组,所以它应该与这个修复一起工作。

编辑

如果这是循环在您的用户/配置文件视图中,您需要@friends在用户控制器的配置文件操作中定义。

def profile
    @username = params[:id]
    @friends = current_user.friends
    @title = "User Profile for #{@username}"
    @user = User.find_by_username(@username)
    @users = User.all :conditions => ["id != ?", current_user.id]
end 

并添加before_filter :authenticate_user!到您的UsersController.

于 2012-07-24T02:38:17.527 回答
1

在 ruby​​ 中,通常会看到for user in @friends要编写的行:

@friends.each do |user|

在此配置中,我们将每个消息传递给存储在 @friends 中的对象,该对象可能为 nil 并引发错误。

确保@friends = current_user.friends在你FriendshipsController没有返回nil(你不能迭代它)。

如果您正在接收nil,请通过将行更改为以下内容来添加默认值:@friends = current_user.friends || []

于 2012-07-24T03:44:16.320 回答