そこで、Amistad からフレンドシップ モデルのビューをセットアップしようとしていますが、「undefined method `each' for nil:NilClass」というエラーが発生し続けます。
これは私のfriendship.rbモデルです
class Friendship < ActiveRecord::Base
include Amistad::FriendshipModel
attr_accessible :user_id, :friend_id
end
これは私の friends_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
誰が何が起こっているのか知っていますか?私はまだレールに慣れていないので、いくつかの洞察を利用できます。
景色:
.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"