Nested layout inside application layout

I have few problems with nested layouts. On my site, I need to make one separate part of the site for the administrator only.

I have this in my file application.html.erb:

<body>
    <%= render 'layouts/header' %>
    <div class="container">
        <%= yield %>
        <%= render 'layouts/footer' %>
    </div>
</body>

I was wondering how I can now add another template like this inside <%= yield %>, because that part of the administrator's me again need a fixed part of the site, such as header, and footerin the main layout. Instead, headerand footerI will have two menus. I want it to <%= yield %>be filled with a new template, which will have a menu at the top and a new <%= yield %>one that will be filled with actions from the administrator controller. That way, the menu will always stay on top.

I made a partial menu views/admins/_menu.html.erb:

<div>  
    <div>  
        <div class="container">  
            <ul>
                <li><%= link_to "Action1", '#' %></li>
                <li><%= link_to "Action2", '#' %></li>
                <li><%= link_to "Action3", '#' %></li>
            </ul>
        </div>  
    </div>  
</div> 

layouts/sublayouts/admin.html.erb:

<%= render 'admins/menu' %>
<%= yield %>

views/admins/_menu.html.erb , .

:

Header/Menu
   |
Container
   |Content
Footer

- :

Header/Menu
   |
Container
   |Content
     |Admin Menu
     |Admin Content
   |
Footer

?

+3
2

:

- application.html.erb.

, Admin Panel .

, , , Admin Panel . - before_filter :

# app/controller/admin_controller.rb
class AdminController < ActionController::Base
  before_filter: set_admin_status

  private
  def set_admin_status
   @admin = true
  end
end

:

# application.html.erb
<body>
    <%= render 'layouts/header' %>
    <div class="container">
        <% if @admin %>
          <%= render 'admins/menu' %>
        <% end %>
        <%= yield %>
        <%= render 'layouts/footer' %>
    </div>
</body>

, , , , Admin Panel , @admin_status , , , , .

+1

:

application_controller.rb

class ApplicationController < ActionController::Base    
  protect_from_forgery
  layout :layout

  private
  def layout
    if self.class.parent == Admin
      'application_admin'
    else
      'application'
    end
  end
end

///application.html.haml

Header/Menu
   |
Container
   |Content
Footer

///application_admin.html.haml

Header/Menu
   |
Container
   |Content
     |Admin Menu
     |Admin Content
   |
Footer

1


/routes.rb

namespace :admin do
  root to: 'home#index'
  resources :admins
end

///admins_controller.rb

class Admin::AdminsController < ApplicationController
  def index
    // code
  end
end
+1

All Articles