| Layouts in Ruby on Rails. Separating Presentation from Data |
|
|
|
| Written by Administrator |
| Monday, 26 July 2010 23:44 |
1. IntroductionIn this article I'll show how easy is to build a website layout using one of the key concepts of Ruby on Rails, the DRY(Don't Repeat Yourself). Following this concept, duplicated code should be always avoided, as there is always a way to put the same code to work in different parts of your application. Take a look at my website for example. There is a header, a dynamic content part in the middle(it seems that i have a left sidebar too, but actually is just floating on the left), and a footer located at the bottom of each page. The header and the footer are always shared between the other pages. However the content part is dynamic and that changes when a visitor click on different links of my website. Ruby on Rails has a very simple and efficient solution to put it all togheter with minimal effort. Ready?! Let's go! 2. The codeAs you might know , Rails works with conventions. One of its conventions is to search for a layout inside a default folder (app/views/layouts). The default folder can be changed as long as you declare it when using the layout command. Now inside the folder app/views I created a folder called "static_content" which will hold the header and the footer information. As part of the convention, a partial always need an undercore symbol ("_") at the beginning of the filename. In my example the header would be called "_header.html.erb" while the footer "_footer.html.erb". Fig 1, shows the applications structure created for this example:
Fig 1 The html.erb extension indicates that you can write pure html code inside as well as Ruby code. For that, you just need to indicate when to use ruby code: E.g "<% my ruby code goes inside %>", after that the Ruby interpreter will handle the code inside that block. Code for mylayout.html.erb
Now for the header and the footer file I wrote the following code: Code for _header.html.erb
Code for _footer.html.erb
Now the views for the dynamic part of the web site. The following are the views for methods content1 and content2 Code for content1.html.erb
Code for content2.html.erb
The brain of the application is the controller. The controller is responsible for all the logic part of a rails application (and for calling the right view). In a rails application a controller has methods and each method has its own view.In this example, the testing_layouts controller, has two methods: content1 and content2. As you can see in fig 1, for each of these methods there is a view with the same name.You don't need to boder about how the method will link to the view. Rails already knows that it should look for the view inside the folder on views, with the same name used by the controller.In our example, when calling method content1, rails will find a file with the same name (content1.html.erb) under the folder (app/views/testing_layouts/) Code for testing_layouts_controller.rb
The code above shows how is possible to link the layout with the controller. Using the command "layout 'mylayout'" you are telling Rails that your views should be presented inside this layout context. Now when you call content1 or content2 , rails will insert the view related to this method where the command "<%= yield %>" is situated. The result when calling content1 is the following:
Fig 2 Then when calling content2:
Fig 3 Now you can imagine how dynamic, fast and easy you can build a web site |
| Last Updated on Friday, 06 August 2010 15:29 |


