Rails Note #5: Dynamic Layouts
Wow.. everyday.. Ruby on Rails seems to amuse me.. I just figured how to seperate layouts just by having different files in the layouts/ which corresponds to a controller. If a layout file “home (home.html.erb)” exists and there is home_controller, then the home_controller would use that layout instead of application.html.erb.
Now.. I haven’t validated this concept by book and would do so soon..
Fig 1
Another way is specifying a filter…
- In application.rb, specify a method that displays the ‘admin’ layout if the user is admin.
def load_layout if admin? self.class.layout('admin') end end |
- home_controller
class HomeController < ApplicationController before_filter :load_layout end |
Fig 2. admin.html.erb now exists in layouts/
Reference:
http://railscasts.com/episodes/125-dynamic-layouts