in routing
resources :departments do
resources :buildings do
resources :halls do
end
resources :chairs do
resources :buildings do
resources :halls do
end
end
end
I want to share the same views of buildings
and halls
between chairs
and departments
. And I am thinking to create url's in views this way:
link_to "Show", [@department, @chair, @building, @hall].compact
If @chair
doesn't exist, it's gonna be nil
, so upper link will generate:
link_to "Show", [@department, @building, @hall]
If @chair
exist:
link_to "Show", [@department, @chair, @buildi开发者_JS百科ng, @hall]
My question is: How to create this way url's to edit
, or new
action? Or maybe there exist better solution for this problem with this kind of nested resources?
You can stick a symbol in the array as well. So something like this:
link_to "Edit", [:edit, @department, @chair, @building, @hall]
I have an application where we nest three up to four resources deep. One example is the management of contacts. We have Company, Location and Contact models.
Usually you can access the new and edit paths with new_department_chair_building_path(@department,@chair)
.
Do not forget to include the hidden ID's from your predecessors (deparment, chair) in the form for a new building.
In your building controller you will have to filter out what objects to load depending on the the context. We do it something like this
# Company controller
def index
if params[:project]
@project = ....
end
end
There may be better ways to do this, but we haven't found any.
In the views we check for @project
and then render partials.
I hope this may somewhat help you.
Cheers,
Dan
精彩评论