2011-08-08 43 views
0

我使用嵌套资源在我的Rails应用程序,像这样:Rails应用继承和嵌套资源

resources :devices, :except => [:edit] do 
    resources :services, :only => [:index, :destroy], :controller => "devices/services" 
    resources :alerts, :only => [:index, :destroy], :controller => "devices/alerts" 
end 

现在我想做的是有设备/显示视图是“布局”为嵌套资源和 我定义任何其他子行动。

所以说,我的设备/ show.html.erb看起来是这样的:

<div class="resource"> 
    <div class="sidebar"> 
    <ul> 
     <li><%= active_link_to "General", @device, :active => :exclusive %></li> 
     <li><%= active_link_to "Services", device_services_path(@device) %></li> 
     <li><%= active_link_to "Alerts", device_alerts_path(@device) %></li> 
     etc.. 
    </ul> 
    </div> 
    <div class="data"> 
    <%= render "shared/flash_messages" %> 
    <%= yield %> 
    </div> 
</div> 

那么,如何去呈现子视图(设备/服务/ index.html.erb,设备/警报/index.html.erb),其中(假设的)产量是在#DATA DIV?

+0

出了什么问题,你有什么? – jamesc

+0

产量仅适用于布局AFAIK。我想这和产量不会呈现什么... –

+1

所以你的问题是关于如何使用收益呢?在这种情况下,你应该看看使用content_for http://guides.rubyonrails.org/layouts_and_rendering.html#using-content_for – jamesc

回答

0

好像你的问题是绝对无关,与继承和嵌套资源。

如果你想知道如何使用产量命令我张贴在我对你的问题的评论的链接,但这里有一个例子专门为你的代码

<div class="resource"> 
    <div class="sidebar"> 
    <ul> 
     <li><%= active_link_to "General", @device, :active => :exclusive %></li> 
     <li><%= active_link_to "Services", device_services_path(@device) %></li> 
     <li><%= active_link_to "Alerts", device_alerts_path(@device) %></li> 
     etc.. 
    </ul> 
    </div> 
    <div class="data"> 
    <%= render "shared/flash_messages" %> 
    <%= yield :nested_resources %> 
    </div> 
</div> 

那么无论你要放置在任何事情div将其封装在content_for:nested_resources块中,例如 在某处视图模板

<% content_for :nested_resources do %> 
    <!-- whatever code you would normally have in your view template --> 
<% end %>