Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 838
Supritha Avatar asked May 07 '15 11:05

Supritha


People also ask

What is HTML ERB file?

As @Chowlett mentioned before, erb stands for Embedded Ruby. When you define any file as ". html. erb" that means it's an HTML file with ruby code embedded in it and it is similar to ". rhtml" extension of rails file.

How should you use nested layouts in Rails?

The layouts removes code duplication in view layer. You are able to slice all your application pages to blocks such as header, footer, sidebar, body and etc. This is an example of typical web application page, almost every site has these blocks. And as a rule the body block differs on each page.


1 Answers

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.

like image 189
steve klein Avatar answered Nov 14 '22 22:11

steve klein