2016-06-07 110 views
-3

我想要做的是創建一個只顯示當前用戶的帖子,興趣和專輯而不是網站上的所有興趣和專輯的索引頁面。要做到這一點,我使用current_user方法在PostsController嘗試,但我不斷收到一個沒有方法錯誤:如何使對象可以在rails中查看當前用戶

def index 
    @posts = current_user.Post.all 
    @albums = current_user.Album.all 
    @interests = current_user.Interest.all 
end 
+0

這個案例很重要.... –

回答

2

您可能需要使用複數和方法名在Ruby中的downcase:

def index 
    @posts = current_user.posts 
    @albums = current_user.albums 
    @interests = current_user.interests 
end 

此外,您需要確保所有這些資源實際上都與用戶相關聯。

+0

當我嘗試得到一個NoMethodError,它說:「未定義的方法'帖子'爲#<用戶:0x594b040>」 – nums

+0

您的數據庫表是否有'user_id'列,並且它們是設置正確的值?你的'用戶'模型是否定義了'has_many:posts'關聯(對於'albums'和'interest'是一樣的)? – spickermann

+0

我剛剛添加了這些,它的工作。謝謝 :) – nums

相關問題