Site-Wide Layouts


A common issue with websites is “how can I create and easily maintain a site-wide look and feel to my website?” Rails answers that question with the MVC (Model-View-Controller) design pattern.

In MVC, the view is completely separate from the model (application domain) and the controller. The view only hooks in to the model and shows relevant data. (In websites, the view also contains the controller aspects–links to interact with the site.)

Rails provides a convenient views folder. It contains sub-folders, one for each model/controller, where you can put model-specific views (such as the show and edit page views). It also contains an extra folder, called layouts.

Layouts provide a place to put any reused layouts. In particular, you can create a site-wide layout easily! Simply add a file to layouts called application.rhtml, and Rails will automatically add this layout to any page request from your site. (Use yield to place the page’s content.)

If you need multiple layouts–such as with a blog, you may have an admin layout for administration pages, and a general layout for user pages–you can add both layout files here.

To add a specific layout to all pages rendered by a controller, simply add layout "layoutname" (without the file extension) to your controllers.

Tags: ,     Posted in Development

Rate this article:
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

Related Content


Leave a Reply