rails 使用 link

编程入门 行业动态 更新时间:2024-10-28 10:23:31
本文介绍了rails 使用 link_to 和命名空间路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一组路线 &具有 admin 命名空间的控制器,我在使用具有这些新路由的链接助手时遇到了一些问题.

I've created a set of routes & controllers with the admin namespace, and I was having some issues using the link helpers with these new routes.

我看到有一些新的路径帮助器,例如 admin_projects_path,它指向/admin/projects.但是,我无法链接到命名空间内这些对象的显示、编辑、销毁等路径.我该怎么做?

I see that there are some new path helpers, such as admin_projects_path which leads to the /admin/projects. however, i'm having trouble linking to the show, edit, destroy, etc. paths for these objects within the namespace. how do I do that?

推荐答案

您应该会看到 rake routes 中列出的所有路由,并且可以通过名称使用这些路由来获得正确的命名空间.在您手动传入 :controller 和 :action 的地方使用自动检测不会像您发现的那样工作.

You should see all of your routes listed in rake routes and you can use those by name to get the proper namespacing. Using the automatic detection where you pass in :controller and :action manually won't work as you've discovered.

如果它在路由中被列为 new_thing,则该方法是带有适当参数的 new_thing_path.例如:

If it's listed as new_thing in the routes, then the method is new_thing_path with the appropriate parameters. For instance:

link_to('New Project', new_admin_project_path) link_to('Projects', admin_projects_path) link_to(@project.name, admin_project_path(@project)) link_to(@project.name, edit_admin_project_path(@project)) link_to(@project.name, admin_project_path(@project), :method => :delete)

更多推荐

rails 使用 link

本文发布于:2023-10-09 17:52:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rails   link

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!