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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With