0xSina
0xSina

Reputation: 21553

Rails layout per controller

I have a home controller and a news controller. I want both of these controller to use the application.html.erb layout file and in addition to that, for home, use the home layout and news, use the news layout. and then render a specific view. Is this possible in rails?

In other words, I don't want to specify a layout per view, but per controller, both inheriting from application.html.erb layout.

What i want to do is remove the redundancy of adding the top navigation bar and including javascript/css in every single layout file. I'd rather include that in one file, and do controller specific layout with another layout, and then finally render the view.

Thanks

Upvotes: 28

Views: 25488

Answers (2)

varatis
varatis

Reputation: 14740

class ProductsController < ApplicationController
  layout "inventory"
  #...
end

http://guides.rubyonrails.org/layouts_and_rendering.html

Upvotes: 13

Tim Peters
Tim Peters

Reputation: 4144

You can tell a controller to use a specific layout, eg

class HomeController < ApplicationController
  layout 'home'
end

class NewsController < ApplicationController
  layout 'news'
end

These will expect the layouts in app/views/layouts/home.html.erb etc

Upvotes: 50

Related Questions