在Rails中显示用户的帖子不起作用

时间:2016-02-19 13:18:38

标签: ruby-on-rails

我得到了未定义的方法`帖子'为零:NilClass错误:
帖子控制器:

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) %>

1 个答案:

答案 0 :(得分:2)

通过在路径

中设置将用户名传递给params
<%= link_to post.user.username, liked_posts_path(username: post.user.username) %>