Rolling with Ruby on Rails Revisited, Part 2
Pages: 1, 2, 3, 4

The requirements say we need a footer on every page that takes us to either the Recipes listing or the Categories listing. The window title and page heading also need to be the same on every page. Let's add all that now.



My cookbook2\app\views\layouts directory contains two files: category.rhtml and recipe.rhtml. I'll delete one (either one) and rename the other to application.rhtml. Now I'll open application.rhtml (Figure 22).

original application layout file
Figure 22. Working with the application layout file

CB: First I'll change the window title...

<title>Online Cookbook</title>

Then I'll add a heading for the page...

<h1>Online Cookbook</h1>

Rails applies what it finds here to all the pages that the application serves. So by adding just a few lines here I'll add the footer we need on every page.

<p><% if params[:controller] == 'recipe' %>
     <%= link_to "Create new recipe", :controller => "recipe", :action => "new" %>
   <% else %>
     <%= link_to "Create new category", :controller => "category", :action => "new" %>
   <% end %>
 
   <%= link_to "Show all recipes", :controller => "recipe", :action => "list" %>

   <%= link_to "Show all categories", :controller => "category", :action => "list" %></p>

Which gives us Figure 23

updated application layout file
Figure 23. Modified application layout file

We save that. OK, Boss. We changed the content of the <title> line and, by my generous approach (counting changed lines, too), we added seven lines of code. You ready to check our progress again?

Boss: Sure, CB. Refresh our browser and... (Figure 24).

browser shows new layout
Figure 24. Application-level results in the browser

Boss: Dammit man!

Paul: Earlier you were using views and partials to tweak the visuals, CB. Now you're using layouts. Could you explain a bit about them?

CB: Sure, Paul. Layouts are a mechanism Rails provides to make it easy to apply the DRY principle to our view code. When we generate the scaffolding for a model/controller pair, Rails assumes that we're probably going to want some commonality in the look and feel for the pages rendered by the methods in the controller. Layouts let us 'wrap' the views. By default, when Rails needs to generate a page, it looks for a view file with the same name as the method. Then it looks further instructions in a layout file with the same name as the controller. If that layout file doesn't exist, then Rails looks for a file called application.rhtml. Using the combination of partials, views, and layouts, Rails gives me an easy way to control what gets presented to the visitor at the method level, the controller level, and at the application level.

CB: Yep. But remember, we didn't get to test the filtering it's supposed to do. Now that we can get there from here, lets go add another category so we can do that. Click the Show all categories link for us (Figure 25).

categories listing
Figure 25. Initial category listing

CB: We have a little cleanup to do, the same stuff we already did for recipes. But before we do that, let's check the behavior. Let's see what happens when I click the link to add a new category. Either link will work fine. (Figure 26)

new category page
Figure 26. New category creation page

We need the same cleanup here, too. Let's stay on point, though, and go ahead and add a new category named "beverages" (Figure 27).

updated category listing
Figure 27. New category added to list

OK. Our new category feature works, so let's clean up the look. We don't have any screen shots of what the Category listing should look like, so I'm just going to assume it's similar to the Recipe listing. Let's go ahead and make that change.

I'll edit the cookbook2\app\views\category\list.rhtml file. It looks pretty much like the list view for recipes. I'm going to make essentially the same changes I made before.

Delete the heading line.

Add the border attribute to the table tag.

<table border="1">

This time I'll leave the table headings alone because there's only one and it seems OK. That means I just need to replace the code that generates the table rows.

<td><%= link_to %Q{#{category.send(column.name)}}, :action => 'show', :id => category %>
   
    <%= link_to '(delete)', { :action => 'destroy', :id => category },
                    :confirm => 'Are you sure?', :post => true %></td>

Then delete the lines below the </table> tag. Now that we're done with that... (Figure 28).

updated category view
Figure 28. Updated category list view file

I'll go ahead and save it.

Pages: 1, 2, 3, 4

Next Pagearrow