2016-01-23 50 views
1

我目前無法列出current_user正在關注或正在關注的所有用戶。我嘗試了至少6種不同的方法讓Users_Controller在視圖中使用方法。接收未定義的acts_as_follower方法all_following和all_follows

這兩種方法/觀點造成的問題將放在以下

def following 
    @user_following = User.all_following(:order => 'created_at DESC').paginate(page: params[:page]) 
    end 

    def followers 
    @user_followers = User.all_follows(:order => 'created_at DESC').paginate(page: params[:page]) 
    end 

用戶關注查看

<% if @user_followers.any? %> 
<% @user_followers.each do |user| %> 
    <%= link_to(image_tag(user.avatar.url(:thumb)), user_path(user)) %> 
    <%= user.username %> 
    <%= user.followers_count %> 
<% end %> 
<% end %> 

用戶以下幾種觀點

<% if @user_following.any? %> 
<% @user_following.each do |user| %> 
    <%= link_to(image_tag(user.avatar.url(:thumb), user_path(user)) %> 
    <%= user.username %> 
<% end %> 
<% end %> 

堆棧跟蹤誤差

Processing by UsersController#followers as HTML 
    Parameters: {"id"=>"john280"} 
    User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]] 
    User Load (0.0ms) SELECT "users".* FROM "users" 
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.0ms) 

NoMethodError (undefined method `all_follows' for #<User::ActiveRecord_Relation:0xd616898>): 
    app/controllers/users_controller.rb:64:in `followers' 

Route.rb

resources :users, only: [:get, :show, :index, :edit, :update], path: '' do 
    member do 
     get :follow 
     get :unfollow 
     get :followers 
     get :following 
    end 
    end 


/:id/follow(.:format)    follow_user_path 
/:id/unfollow(.:format)   unfollow_user_path 
/:id/followers(.:format)   followers_user_path 
/:id/following(.:format)   following_user_path 

回答

1

acts_as_follower寶石,它看起來像all_follows是一個實例方法,但在這裏你調用它的類。在致電all_follows之前,您需要找到一個特定的User

您是否有特定的用戶來查找以下內容?或者你是否試圖在數據庫中查找所有內容?

由於您的路線是多個資源(與路徑設置爲「」),我假設你要的頁面像/123/follows,所以例如你的控制器方法可能是這樣的:

def user 
    @user ||= User.find(params[:id]) 
end 

def following 
    @user_following = user.all_following(:order => 'created_at DESC').paginate(page: params[:page]) 
end 

def followers 
    @user_followers = user.all_follows(:order => 'created_at DESC').paginate(page: params[:page]) 
end 

如果是這種情況,那麼你的意見不應該改變。

+0

嗯,好的 - 我知道,'all_follows'方法作用於'User'的實例,而不是'User'類。你需要一個特定的用戶來調用該方法。相應地更新了答案。 – rusty

+0

該方法應該已經暴露給用戶。關注/取消關注按鈕正常工作。 –

+0

這只是一個在'user'而不是'User'上調用它的問題嗎? – rusty