Search code examples
ruby-on-railsrubynomethoderror

How to make objects viewable for current user in rails


What I'm trying to do is make an index page that only shows the current user's posts, interests and albums instead of all interests and albums on the site. To do this I've tried using the current_user method in the PostsController but I keep getting a no method error:

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

Solution

  • You probably have to use the plural and method names are downcase in Ruby:

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

    Furthermore you need to ensure that all these resources are actually associated with the user.