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

I have a controller called "Pages" with about 5 pages (views) for which I created a layout called "page.html.erb". So my Pages controller has:

class PagesController < ApplicationController
layout 'page' 

      

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

+3


source to share


2 answers


If you don't specify a layout in your controller, Rails will render your layout by default application

. If you follow this convention, you can use a application.html.erb

site page for your general 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 displayed.

Controller actions by default will render their respective views. For example, if you have an action foo

in a controller bars_controller.rb

, Rails will render /app/views/bars/foo.html.erb

unless you redirect or define another view to render in the action. In fact, if all you want to do in the action foo

is to render the page, you don't even need to define the action in your controller!



My friend's configuration convention.

+3


source


I usually break my layout into smaller particles (header, footer, HTML HEAD, etc.). This way I can use multiple layouts by mixing different particles together.



+3


source







All Articles