如何将多个参数传递给Rails中的嵌套路径路径?

Cu1*_*ure 5 routes ruby-on-rails path link-to

我是Rails的新手,通常link_to为正常的unnested路由设置一个帮助器,如下所示:

link_to "Delete", article_path(article.id), method: :delete, data: {confirm: "Are you sure?"}
Run Code Online (Sandbox Code Playgroud)

但是我正在学习嵌套路由,似乎我需要提供带有两个参数的路径,例如:

link_to "(Delete)", article_comments_path(comment.article_id, comment.id), method: :delete, data:{comfirm: 'Are you sure?'}
Run Code Online (Sandbox Code Playgroud)

然而,这似乎不起作用.我已经看到你可以这样格式化link_to:

link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: { confirm: 'Are you sure?' }
Run Code Online (Sandbox Code Playgroud)

但这似乎让我感到困惑,因为没有定义路径,路径所需的值也没有直接指定.

是否可以格式化嵌套link_to路径,如上面的unnested路径,还是需要格式化,如第三个例子所示?如果是这样,有人可以尝试解释如何将此数组格式转换为Rails执行的URL吗?

谢谢

路线:

article_comment_path - DELETE - /articles/:article_id/comments/:id(.:format) - 评论#stroy

Man*_*eep 5

我认为你的路线可能是articles/:article_id/comments/:id这样的:

<%= link_to "Delete", article_comments_path(article_id: comment.article_id, id: comment.id), method: :delete, data:{comfirm: 'Are you sure?'} %>
Run Code Online (Sandbox Code Playgroud)

你的第三个链接应该是

<%= link_to 'Destroy Comment', polymorphic_path(comment.article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
Run Code Online (Sandbox Code Playgroud)

详情请查看 Polymorphic routes

更新

你只需要将本地人传递给你的部分:

<%= render "comment", locals: {article: comment.article, comment: comment} %>
Run Code Online (Sandbox Code Playgroud)

然后使用

<%= link_to "Delete", article_comments_path(article.id,comment.id), method: :delete, data:{comfirm: 'Are you sure?'}%>
Run Code Online (Sandbox Code Playgroud)