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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With