Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing a default layout for views within a namespace

I have a series of controllers and associated views within my app that exist inside the Admin namespace. I want these views to use a different layout than those in the main namespace so I can give them a different appearance to the regular site. Is there a way I can do this using Rails' built in functionality? I know that if I have a PostController class and create a posts.html.erb view in the layouts directory this will be used as the layout for any of the controller's views, is there something similar I can do for namespaced controllers?

like image 954
rpowell Avatar asked Nov 10 '11 13:11

rpowell


2 Answers

Create custom AdminController class for your admin's controllers that you're going to inherit from. This AdminController inherits from ApplicationController and overrides layout.

class AdminController < ApplicationController   
  layout "admin" 
end

class Admin::PostController < AdminController 
end
like image 100
sparrovv Avatar answered Oct 25 '22 20:10

sparrovv


There is not need in layout method. You should put your layout files for namespace into views/layouts/<namespace>.

Example namespace with name 'foo':

Controller: app/controllers/foo/bar_controller.rb

Layout for this controller: app/views/layouts/foo/bar.html.erb

like image 38
Alexander Sulim Avatar answered Oct 25 '22 19:10

Alexander Sulim