如何在Rails中将多个参数传递给嵌套的路由路径? [英] How do you pass multiple arguments to nested route paths in Rails?

查看:98
本文介绍了如何在Rails中将多个参数传递给嵌套的路由路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的新手,通常为普通的非嵌套路由设置link_to帮助器,如下所示:

I am new to Rails and normally set up a link_to helper for a normal unnested route like so:

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

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

However I am learning about nested routes and it seems I need to provide the path with two arguments for example:

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

但是,这似乎不起作用.我已经看到您可以像这样格式化link_to:

However this does not seem to work. I have seen that you can format the link_to like this:

link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: { confirm: 'Are you sure?' }

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

But this seems confusing to me as there is no path defined and the values necessary for the path arn't directly specified either.

是否可以像上面的未嵌套路径一样格式化嵌套的link_to路径,还是需要如第三个示例中所示对其进行格式化?如果可以的话,有人可以尝试解释如何将这种数组格式转换为URL以供Rails执行吗?

Is it possible to format a nested link_to path like the unnested one above or does it need to be formatted as shown in the third example? If so could someone try to explain how this array format is translated into a url for Rails to execute?

谢谢

路线:

article_comment_path-删除-/articles/:article_id/comments/:id(.:format)-comments#destroy

article_comment_path - DELETE - /articles/:article_id/comments/:id(.:format) - comments#destroy

推荐答案

我认为您的路线类似于articles/:article_id/comments/:id,因此您可以这样做:

I think your route would be something like articles/:article_id/comments/:id so you can do:

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

您的第三个链接应该是

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

有关详细信息,请检查 Polymorphic routes

For details check Polymorphic routes

更新

您只需要将本地人传递给您的部分人,例如:

You just need to pass locals to your partial like:

<%= render "comment", locals: {article: comment.article, comment: comment} %>

然后使用

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

这篇关于如何在Rails中将多个参数传递给嵌套的路由路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆