Search code examples
ruby-on-railsrubyruby-on-rails-2

Rails - How do I use application.html.erb in my custom layout?


I have a Controller called "Pages" with around 5 pages (views) for which I have rendered a layout called "page.html.erb". So my "Pages" Controller has:

class PagesController < ApplicationController
layout 'page' 

I want my "page.html.erb" layout to use "application.html.erb" by default. How do I do make my custom layout "page.html.erb" to automatically have "application.html.erb" inherited/rendered in it?


Solution

  • If you don't specify a layout in your controller, Rails will render your application layout by default. If you follow this convention, you can use application.html.erb for your overall site page structure (also a good place to include stylesheets and javascript). You can then use = yield in your layout to specify where your controller views should be rendered.

    Controller actions by default will render their corresponding views. For example, if you have an action foo in controller bars_controller.rb, Rails will render /app/views/bars/foo.html.erb unless you redirect or specify a different view to render in the action. In fact, if all you want to do in action foo is render the page, you don't even need to define the action in your controller!

    Convention over configuration my friend.