Rails动作继承和嵌套资源

时间:2011-08-08 17:30:14

标签: ruby-on-rails ruby-on-rails-3

我在我的Rails应用程序中使用嵌套资源,如下所示:

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

现在我要做的是让devices / show视图成为嵌套资源的“布局” 我定义的任何其他子动作。

所以说我的设备/ 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>

那么如何渲染子视图(devices / services / index.html.erb,devices / alerts / index.html.erb),其中(假设的)收益率在#data div中?

1 个答案:

答案 0 :(得分:0)

似乎你的问题与继承和嵌套资源完全无关。

如果您想知道如何使用yield命令,我在您的评论中发布了一个链接,但这里有一个专门针对您的代码的示例

<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 %>