【问题标题】:Commenting on friendly_id posts in rails 4评论 rails 4 中的friendly_id 帖子
【发布时间】: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


    【解决方案1】:

    创建评论时您没有传递post_id。错误说明:id 为零,因为params[:post_id] 没有被发送。在您的评论表单中,请务必设置 post_id 以获取新记录。

    【讨论】:

    • 谢谢你的帮助,你能举个例子吗?对 ruby​​ 和 rails 来说还是很新的。是不是类似于在实际评论表单中设置隐藏字段?
    • 这绝对是一种方法。 f.hidden_field :post_id, value: @post.id 其中@post 是您要附加评论的帖子。
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2019-06-13
    • 1970-01-01
    • 2013-04-01
    相关资源
    最近更新 更多