Ruby on Rails 2.1 Layouts - Ruby on Rails 2.1

What is Layouts in Ruby on Rails? How create Layouts?

Layout is a design, which describes the surroundings of an HTML page. Layout describes common look and feel of final output. Layout files exist in in app/views/layouts subdirectory.

Layout process includes defining a layout template and letting the controller know that layout is exists and can be used.

Let's create a template by adding a new file named standard.html.erb and save in app/views/layouts. User should let controllers know to use the created template with the name of the file. Recommended format of naming is standard.html.erb.

Now add the below code to the new standard.html.erb file and save the changes

The above added code was standard HTML elements except two lines. The stylesheet_link_tag helper method outputs a stylesheet <link>.

Here, we are linking style.css and style sheet, which is the name given in double quotes in front of stylesheet_link_tag command.

The yield command lets Rails know, which to keep the generated HTML here for the method called.

Open already saved controller book_controller.rb file, which is existing in app/controllers subdirectory and add the following bold line just below the first line as shown below

This process let controller know that user wants to use a layout existing in standard.html.erb file. Once this is done, try browsing books and should see below screen

layout-example

Adding Style Sheet

Rails is using its own default style sheet to show the result , as we haven’t created any style sheet yet.

Let us create a style sheet called style.css and save it in application subdirectorty ~/library/public/stylesheets and add the below code to this file.

Once the code has updated, refresh browser and see the difference. Now the style sheet should be updated and it should perfectly look like below

layout-example

What is Next?

We have learned about how to develop an application and its cycle using Ruby on Rails 2.1. User can easily start creating their own applications with the topics discussed until now.

In next chapters lets learn about, how we develop application with Rails Scaffoldingconcept?

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Ruby on Rails 2.1 Topics