【发布时间】:2014-06-11 21:54:01
【问题描述】:
我刚刚为我的帖子 url 实现了friendly_id,但是创建了 cmets 错误,“没有路由匹配 {:action=>"show", :controller=>"posts", :id=>nil} 缺少必需的键:[ :id]"
cmets 控制器
before_filter :authenticate_user!
def create
@comment = Comment.new(comment_params)
@comment.post_id = params[:post_id]
@comment.user_id = current_user.id
@comment.save
redirect_to post_path(@comment.post)
end
def comment_params
params.require(:comment).permit(:user_id, :body)
end
我已经测试过删除friendly_id,这肯定是搞砸了。我认为这是因为在寻找 url/posts/post-id/cmets 时创建评论会指向 url/posts/post-name/cmets (friendly_id)。
有什么想法吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 comments friendly-id