如何在命名空间和根路径的路由中拥有一个资源 - Rails 4 [英] How to have one resource in routes for namespace and root path altogether - Rails 4

查看:35
本文介绍了如何在命名空间和根路径的路由中拥有一个资源 - Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在命名空间admin"中创建自定义管理面板.

I am making a custom admin panel in a namespace "admin".

我在该命名空间中有资源课程".

I have resources "courses" within that namespace.

但我还想要一条通往不在该命名空间中的课程"的路线:

But I would also like a route to "courses" that is not in that namespace:

例如:BOTH localhost:3000/admin/courseslocalhost:3000/courses

eg: BOTH localhost:3000/admin/courses AND localhost:3000/courses

如果这需要不同的控制器也没关系.

It's OK if this requires different controllers.

我担心的是,如果我在同一条路线上拥有两种资源,那它就不是真的干了.

My concern is that its not really DRY if i have both resources for the same route.

namespace admin do
   resources :courses
end

而且只是

resources :courses

有没有办法让一个资源在命名空间和没有命名空间之间共享,或者上面的例子是可行的方法?

Is there a way to have one resource be shared between namespace and without namespace, or is the example above the way to go?

推荐答案

哦等等!还有可能使用关注

Oh wait ! There's also the possibility to use concerns !

concern :shared_actions do
   resources :courses
   resources :something_else
end


namespace :admin do
   concerns :shared_actions
end
concerns :shared_actions # Will add it to the root namespace ^^

显然这就是这个人也尝试过的做 :D

EDIT : apparently this is what this guy also tried to do :D

这篇关于如何在命名空间和根路径的路由中拥有一个资源 - Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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