所以我试图从 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"