NoMethodError未定义的方法`followers_count'在users / show中

时间:2014-08-18 18:59:57

标签: ruby-on-rails ruby-on-rails-4 controller nomethoderror

我浏览了Treehouses教程,了解如何在添加和删除朋友时建立用户友谊。但是,现在我想说明用户关注的人数以及关注该用户的人数。 Michael Hartl的RoR教程对此进行了讨论,但我不知道如何在Treehouses设置的情况下实现他的教程。我是初学者,我知道在引用用户数时我的控制器是不正确的。我得到一个NoMethodError未定义的方法`followers_count'在users / show。

是否有人或有人成功地这样做了?如果有的话,任何关于去哪里看的指示或我如何做到这一点将不胜感激!谢谢。

用户/模型

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :posts
  has_many :user_friendships
  has_many :friends, through: :user_friendships, 
                     conditions: { user_friendships: { state: 'accepted'} }

  has_many :pending_user_friendships, class_name: 'UserFriendship',
                                      foreign_key: :user_id,
                                      conditions: { state: 'pending' }
  has_many :pending_friends, through: :pending_user_friendships, source: :friend





  has_many :requested_user_friendships, class_name: 'UserFriendship',
                                      foreign_key: :user_id,
                                      conditions: { state: 'requested' }
  has_many :requested_friends, through: :pending_user_friendships, source: :friend




  has_many :blocked_user_friendships, class_name: 'UserFriendship',
                                      foreign_key: :user_id,
                                      conditions: { state: 'blocked' }
  has_many :blocked_friends, through: :pending_user_friendships, source: :friend



  has_many :accepted_user_friendships, class_name: 'UserFriendship',
                                      foreign_key: :user_id,
                                      conditions: { state: 'accepted' }
  has_many :accepted_friends, through: :pending_user_friendships, source: :friend



  validates :name, presence: true



  has_one :profile

  belongs_to :user

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "150x150>" }, :default_url => "/assets/avatar.png"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

 def has_blocked?(other_user)
  blocked_friends.include?(other_user)
 end

 def followers_count
    user_friendships.count
  end

  def followed_by_count
    UserFriendship.where(friend_id: self.id).count
  end

end

用户/显示。 HTML

 <% provide(:title, @user.name) %>

 <div class="row">
   <aside class="span4">
    <section>
       <h1>
         <%= image_tag @user.avatar.url(:thumb) %>
         <%= @user.name %>
         <%= @user.followers_count%>
         <%= @user.followed_by_count %>
        </h1>
     <div id="friend-status">
       <% if current_user.friends.include?(@user) ||  current_user.pending_friends.include?(@user) %>
          <%= link_to "Edit Friendship",  edit_user_friendship_path(friend_id: @user), class: "btn btn- info btn-sm" %>
       <% else %>
          <%= link_to "Follow",  new_user_friendship_path(friend_id: @user), class: "btn btn- info btn-sm", id: 'add-friendship', data: { friend_id:  @user.to_param } %>
        <% end %>
     </div>
    </section>
    </aside>
</div>

用户友谊控制器

class UserFriendshipsController < ApplicationController
   before_filter :authenticate_user!
   respond_to :html, :json

  def index
  @user_Friendships = UserFriendshipDecorator.decorate_collection(friendship_association.all)
  respond_with @user_Friendships
  end   

 def accept
  @user_friendship = current_user.user_friendships.find(params[:id])
   if @user_friendship.accept_mutual_friendship!
  @user_friendship.friend.user_friendships.find_by(friend_id: current_user.id).accept_mutual_friendship!
  flash[:success] = "You are now friends with #{@user_friendship.friend.name}!"
   redirect_to user_friendships_path
    else
    flash[:error] = "That friendship could not be accepted."
    end
   end

  def block
   @user_friendship = current_user.user_friendships.find(params[:id])
    if @user_friendship.block!
      flash[:success] = "You have blocked #{@user_friendship.friend.name}."
    else
      flash[:error] = "This friendship could not be blocked."
    end
    redirect_to user_friendships_path
  end

  def new
    if params[:friend_id]
      @friend = User.find(params[:friend_id]).first
      raise ActiveRecord::RecordNotFound if @friend.nil?
      @user_friendship = current_user.user_friendships.new(friend: @friend)
    else
      flash[:error] = "Friend required."
    end
  rescue ActiveRecord::RecordNotFound
    render file: 'public/404', status: :not_found
  end

  def create
    if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
   @friend = User.find(params[:user_friendship][:friend_id])
   @user_friendship = UserFriendship.request(current_user, @friend)
   respond_to do |format|
     if @user_friendship.new_record?
       format.html do
         flash[:error] = "There was a problem creating this friend request."
         redirect_to user_path(@friend)
       end
       format.json { render json: @user_friendship.to_json, status: :precondition_failed }
        else
        format.html do
          flash[:success] = "Friend request sent."
          redirect_to user_path(@friend)
         end
          format.json { render json: @user_friendship.to_json }
         end
      end
    else
     flash[:error] = "Friend required"
     redirect_to root_path
    end
  end

  def edit
  @friend = User.find(params[:id])
 @user_friendship = current_user.user_friendships.find_by(friend_id: @friend.id).decorate
 end

 def destroy
  @user_friendship = current_user.user_friendships.find(params[:id])
   if @user_friendship.destroy
   flash[:success] = "Your friendship was deleted"
  end  
   redirect_to user_friendships_path
  end

 def user_friendship
   params.require(:user_friendship).permit(:user_id, :friend_id, :user, :friend, :state, :user_friendship)
 end  

  def avatar
  end

  def followers_count
   @user_friendship.find_by_follower_id(friend.id)
  end

  def followed_by_count
   @user_friendship.create!(friend_id: friend.id)
  end

  private
  def friendship_association
    case params[:list]
   when nil
   current_user.user_friendships
   when 'blocked'
   current_user.blocked_user_friendships
   when 'pending'
    current_user.pending_user_friendships
   when 'accepted'
   current_user.accepted_user_friendships
   when 'requested'
   current_user.requested_user_friendships
   end
  end
 end

1 个答案:

答案 0 :(得分:1)

看起来您正在将User模型方法与关系模型方法混合使用。您正在调用@user.followers_count正在寻找用户的followers_count方法,该方法不存在。您已在UsersFriendships模型上定义它。你没有包含User模型,但看起来应该是这样的,以消除这个错误,尽管在你过去之后可能会有更多错误。

class User < ActiveRecord::Base
  has_many :user_friendships
  #everything else

  def followers_count
    user_friendships.count
  end

  def followed_by_count
    UserFriendship.where(friend_id: self.id).count
  end
end

您需要从followers_count模型中删除followed_by_countUserFriendship。此外,看起来您在控制器中有一些私有方法,也称为followers_countfollowed_by_count