所以我试图从Amistad为我的友谊模型设置一个视图,但我一直收到这个错误:“未定义的方法`每个'为nil:NilClass”
这是我的friendship.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"
答案 0 :(得分: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
。
答案 1 :(得分:1)
在ruby中,通常会看到要写的for user in @friends
行:
@friends.each do |user|
在此配置中,我们将每条消息传递给存储在@friends中的对象,该对象可能为nil并抛出错误。
确保@friends = current_user.friends
中的FriendshipsController
未返回nil
(您无法对其进行迭代)。
如果您收到nil
,请通过更改以下内容添加默认值:@friends = current_user.friends || []