1

I have an application.html.erb in views/layouts as usual, with <%= yield %> for content. Now I'm writing a settings page for user accounts which holds a bunch of different pages (profile, account, notifications, etc).

controllers/settings_controller.rb

class SettingsController < ApplicationController
end

controllers/settings/account_settings_controller.rb

class Settings::AccountSettingsController < ApplicationController
end

controllers/settings/profile_settings_controller.rb

class Settings::ProfileSettingsController < ApplicationController
end

For every controller that is part of the Settings namespace I'd like to "always render a view", in particular views/settings/master.html.erb which contains

<markup>
  <%= yield(:settings_content) %>
</markup>

So for example the view settings/profile_settings/edit.html.erb would contain

<% content_for(:settings_content) do %>
  <markup>
  </markup>
<% end %>

I'm not sure where to start. Perhaps my controllers should look like

class Settings::AccountSettingsController < SettingsController
end

Any guidance is most welcome.

EDIT:

Rendered settings/account_settings/edit.html.erb within layouts/application (19.6ms)

should become

Rendered settings/master.html.erb within layouts/application (19.6ms) 
Rendered settings/account_settings/edit.html.erb within settings/master (19.6ms)
Emil Ahlbäck
  • 6,085
  • 8
  • 39
  • 54

2 Answers2

0

I suggest to use this to have a master layout with nested sub layouts using haml:

add this method to your application_helper.rb

  # Allows easy using nested layouts
  def inside_layout(layout = 'application', &block)
    render :inline => capture_haml(&block), :layout => "layouts/#{layout}"
  end

layouts/application.html.haml

!!!
%html
  %head
    -# your header content
  %body
    .content
      = yield

layouts/single_column.html.haml

= inside_layout do
  .middle
    = yield        

layouts/two_column.html.haml

= inside_layout do
  .left
    -# your shared left content
  .right
    = yield        

the column layouts can now be used like normal layouts, but they are nested into the master yield. you can even create more layouts nested in the nested layouts if you name the layout in the inside_layout call.

hope it helps :)

Severin Ulrich
  • 421
  • 4
  • 13
0

write layout 'settings/master in the settings_controller, and you need to inherit namespaced controllers from this one

Anatoly
  • 15,298
  • 5
  • 53
  • 77
  • That is sort of what I am using now. The problem is that I want settings/master to 'inherit' layouts/application, so that <%= yield %> spews out the contents of settings/master. I'm using a very poor solution at the moment so instead of answering my own question I'm waiting for alternate ways to solve this. – Emil Ahlbäck Jul 20 '11 at 08:07