如何使 rails 中的当前用户可以查看对象

How to make objects viewable for current user in rails

我想做的是制作一个索引页面,只显示当前用户的帖子、兴趣和相册,而不是网站上的所有兴趣和相册。为此,我尝试在 PostsController 中使用 current_user 方法,但我一直收到无方法错误:

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

在 Ruby:

中,您可能必须使用复数并且方法名称是小写的
def index 
  @posts = current_user.posts 
  @albums = current_user.albums 
  @interests = current_user.interests 
end

此外,您需要确保所有这些资源实际上都与用户相关联。