25

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

port5432
  • 5,889
  • 10
  • 60
  • 97
0xSina
  • 20,973
  • 34
  • 136
  • 253
  • I think you can just remove the home.html.erb & news.html.erb files from the layouts directory & application.html.erb will be used by default. I think you are suffering from one of the negative aspects of using rails scaffolding (generates the layout files by default). – Brian Jul 03 '12 at 00:35

2 Answers2

46

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

Tim Peters
  • 4,034
  • 2
  • 21
  • 27
  • Yes, but I want it to use application layout, then yield inventory layout, and then render specific view. – 0xSina Jul 03 '12 at 00:35
12
class ProductsController < ApplicationController
  layout "inventory"
  #...
end

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

varatis
  • 14,494
  • 23
  • 71
  • 114
  • Yes, but I want it to use application layout, then yield inventory layout, and then render specific view. – 0xSina Jul 03 '12 at 00:35