Mark  Ruseev
Mark Ruseev

Reputation: 65

Show user's posts in Rails not working

I get undefined method `posts' for nil:NilClass error:
Posts controller:

def liked
 @user = User.find_by_username(params[:username])
 @posts = @user.posts
 render action: :index
end

Index.html.erb:

<%= link_to post.user.username, liked_posts_path(post.user.username) %>

Upvotes: 0

Views: 99

Answers (1)

Azat Gataoulline
Azat Gataoulline

Reputation: 737

Pass username to params by setting it in the route

<%= link_to post.user.username, liked_posts_path(username: post.user.username) %>

Upvotes: 2

Related Questions