Rails命名空间与嵌套资源 [英] Rails Namespace vs. Nested Resource
问题描述
假设我的应用有两种模型,即Foo和Bar.
Let's say my app has two models, Foo and Bar.
Foo(可选)belongs_to Bar.
Foo optionally belongs_to Bar.
现在,我可以查看单个Foo,或搜索特定的Foo,然后FoosController会处理所有这些.我的网址就像:
foos/1
和foos/new
Right now I can look at a single Foo, or search for a particular Foo, and the FoosController handles all that. My URLS are like:
foos/1
and foos/new
有时候我想看看酒吧. BarsController可以处理此问题,而我得到的像是:
bars/1
或bars/1/edit
.
Sometimes I want to look at a Bar. The BarsController handles that, and I get to it like:
bars/1
or bars/1/edit
.
如果我正在查看一个栏,则可能要浏览该栏的所有Foos.因此,我想使用bars/1/foos/
来查看那些Foos.
If I'm looking at a Bar I might want to browse all the Foos that are part of that Bar. So, I'd like to use bars/1/foos/
to look at those Foos.
这对于嵌套资源非常简单,看起来像这样:
This is pretty straightforward with nested resources, and it looks like this:
resources :foo
resources :bar do
resources :foo
end
但是,作为酒吧一部分的Foos与常规的Foos有所不同.因此,例如,如果我加载foos/1
或bars/1/foos/1
,则我将查看同一Foo,但是在每种情况下,我专注于不同的信息.
However, Foos that are part of a Bar are kind of special, set apart from regular Foos. So, for instance, if I load foos/1
or bars/1/foos/1
, I would be looking at the same Foo, but I am focused on different information in each case.
因此,我一直在考虑让BarFoos控制器在Bar上下文中处理Foos.但是,如果我将BarFoos嵌套在Bar下,那么我的助手将像bar_bar_foos_path
和new_bar_bar_foo_path
一样.这似乎是多余的.
So I've been thinking about having a BarFoos Controller to handle Foos when they're in the context of a Bar. However, if I nest BarFoos under Bar, then my helpers are going to be like bar_bar_foos_path
and new_bar_bar_foo_path
. That seems redundant.
因此,现在我正在考虑名称空间,这是我之前从未研究过的.我在rails指南中看到可以定义的内容:
So, now I'm thinking about namespaces, which is something I've never looked into before. I see in the rails guide that I could define:
namespace "bar" do
resources :foos
end
如果这样做,我可以在app/bar/
下创建第二个FoosController
,并且FoosController可以使用诸如bar_foo_path(:id)
而不是bar_bar_foo_path(:id)
的好帮手在Bar内部处理Foos.
If I do that I can make a second FoosController
under app/bar/
, and that FoosController can handle Foos inside of a Bar with nice helpers like bar_foo_path(:id)
instead of bar_bar_foo_path(:id)
.
但是,如果我这样做了,我的BarsController
会怎样?如果我有namespace "bar"
而不是resources :bars
,如何将请求路由到BarsController
?
But if I do that, what happens to my BarsController
? How do requests get routed to BarsController
if instead of resources :bars
I have namespace "bar"
?
最后,我在二级FoosController中需要做些特别的事情以确保与顶级FoosController不会发生名称冲突吗?我意识到路由说命名空间",但是红宝石代码的其余部分如何知道app/bar/foos_controller
和app/foos_controller
不是同一类?
And, lastly, is there anything special I need to do inside my secondary FoosController to make sure there's not a name conflict with the top-level FoosController? I realize the routing says "namespace", but how does the rest of the ruby code know that the app/bar/foos_controller
and app/foos_controller
are not the same class?
谢谢!
推荐答案
我认为您要实现的目标是:
I think what you're trying to achieve is:
- 酒吧有很多
- 查看属于Bar的Foos
- 查看所有Foos,无论其父级如何.
您可以通过以下方法实现: routes.rb:
You can achieve that with: routes.rb:
resources :foos
resources :bars do
resources :foos, :controller => 'bars/foos'
end
您最终得到的路线帮助者是:
The route helpers you end up with are:
- bars_path
- foos_path
- bars_foos_path
- 等,等等,其余为耙路" =)
- bars_path
- foos_path
- bars_foos_path
- etc, etc, 'rake routes' for the rest =)
从本质上讲,您最终得到:
In essence, you end up with:
- app/BarsController(导轨g控制器栏)
- app/FoosController(用于g控制器foos)
- app/bars/FoosController(rails g控制器bar/foos)
在FoosController中,您将照常通过以下方式访问foos:
In FoosController, you would access foos as usual with:
@foos = Foos.all
在bar/FoosController中,您将使用以下命令访问bar的foos:
and in bars/FoosController, you would access bar's foos with:
@foos = @bar.foos
可以使用以下命令在bar/foos控制器中预先检索bar:
where bar can be pre-retrieved in the bars/foos controller with:
before_filter :get_client
private
def get_client
@bar = Bar.find(params[:bar_id])
end
希望这会有所帮助. =)
Hope this helps. =)
修改: 至于命名空间路由,当我从子路径中检索到一些资源时,我就亲自使用它们.例如,如果我的网站上有一个管理"部分,则可能具有以下内容:
Edit: As for namespaced routes, I've personally used them when I some of my resources retrieved from a sub-path. For example, if I have an admin section of my site, then I might have the following:
routes.rb:
routes.rb:
namespace :admin do
resources :foos
end
然后我用以下命令创建控制器:
and I create my controller with:
rails g controller admin/foos
这将设置我的foos资源,以便我可以在我的网站网址"/admin/foos中访问它,并获得诸如admin_foos_path之类的帮助程序.
This sets up my foos resource, such that I can access it at "my site url"/admin/foos, and also get helpers such as admin_foos_path.
这篇关于Rails命名空间与嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!