I try one of a programing style in Rails. which is called a super layout. (I made that name up because I don’t know how to call it)

anyway I realized that I don’t want to have separate layout for every controller. so I ask my friend “Phillipe” how can I just have only one layout and every controller using it.

The solution is (not really a solution though, haha!)

1. As you already know that all controllers has a super class called “ApplicationController”

2. Phillipe suggested that we should create a layout called “application” for ApplicationController

3. And the last thing, In each controller you don’t need to set a layout for them.


figure 1.1 show you there is a application layout in “view”

*Actually in the controller you don’t need to set layout for each controller unless you want to use another layout. okay I will give you an example

Controller View/layouts Model
application application
login_controller admin

assume that we have things like this in our project. I just want to show you how controller interact with their layouts

1. As you can see that login_controller does not have its own layout. If we didn’t set any layout for it. login_controller will use application layout automatically because login_controller is inherited from ApplicationController. And ApplicationController has its own layout called application.

2. For admin_controller, you don’t need to set a layout either. because we already have a layout for admin_controller called “admin”. So admin_controller will use their own controller instead of using one from ApplicationController

3. You might want to know why controllers know which layout are they using. the reason is really simple

* if we didn’t set any layout for controller, the controller will find the layout in folder “Views/layouts”

* look for layout that has exactly same name with the controller and use it (you need to named it exactly same name with controller. like login_controller => layout should be “login”)

* if the controller didn’t find their own layout. then the second thing that the controller might look for is the layout of ApplicationController

I hope you might get a concept for using layout and one last thing that I have a problem with using a super layout which is how do I know what controller is using this layout right now.

I think this method might really helpful for checking what controller is using this layout.

>> controller_name()

This method will return a name of controller that you are on right now.


<% if controller.controller_name == “admin”%>
<%= render :partial => “admin_navigate”%>
<% end %>

The meaning of that code is: if this is a admin_controller then render a admin’s navigator.

P.S. I am not sure this is a good idea to doing this or not but it is another way for programming on rails that I want to share my experiences to the world. HAHAHA!

Created by “WhenURnotAround”