rails 使用 link_to 和命名空间路由 [英] rails using link_to with namespaced routes

查看:54
本文介绍了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_to 和命名空间路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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