Main Content

Multiple views in a single appication - sharing common parts of the template - Ruby on Rails

Archive - Originally posted on "The Horse's Mouth" - 2012-06-23 11:09:33 - Graham Ellis

So far ... we have installed Rails, and written a simple application with a single model, controller and view. In this article, I'll show you how to add a second controller method, and view, while sharing much of the view template. The example takes the scenario of a database of products where you can display either a summary listing of all the products, or all the details of a single product.

For multiple diplays

1. Added method(s) to controller, with links between

The modified code becomes:

  def index
    @main_result = "Gypsy recomends the Squirrel Pie"
    stockdata = Product.all
    @info = "<br />"
    for stock in stockdata
      @info += "<a href=\"single?name=#{stock.pname}\">[L]</a> " + stock.pname + " ... " + "<br />"
    end
  end
  
  def single
    @main_result = "<a href=/productlister/>[all]</a>"
    stockdata = Product.all
    @info = "<br />"
    stockdata.each do |stock|
      if stock.pname == params[:name] # Params is from a "form"
        @info += stock.pname + " full record " + "
"
      end
    end
  end


2. Added an overall layout for every view

This file is app/views/layouts/productlister.erb

  <html>
  <head>
  </head>
  <body>
  This is the general template for all productlister application pages
  <hr />
  <%= yield %>
  <hr />
  Copyright and common stuff!
  </body>
  </html>


3. REDUCED the index.erb to contain only the bits that differ on different pages

  <h1>All records report</h1>
  Here is a message: <b><%= @main_result %></b><br /><br />
  Here are the available products: <b><%= @info %></b><br /><br />


and add single.erb to contain the different VIEW bits for the single page

  <h1>Single Record Report</h1>
  To revert to product list: <b><%= @main_result %></b><br /><br />
  All about this product: <b><%= @info %></b><br /><br />


To note:

a) use of params to pick up from form

b) The main template for all output pages is now app/views/layouts/productlister.erb and app/views/productlister/index.erb has become a subsidiary pulled in by <%= yield %>

Diagram shows how the view selection works - for our application called productlister and two views called single and index.

1. Black line; if there is a file called /app/views/layouts/productlister.erb then THAT file will be used, filling in at the request to yield with app/views/productlister/single.erb or app/views/productlister/index.erb.

2. Red line; if there is NOT a file called /app/views/layouts/productlister.erb, then the file app/views/productlister/single.erb or app/views/productlister/index.erb will be used.

The idea is that the application wide part of the view goes in the file in the layouts directory, and the part of the view that only applies to some of the controllers goes in the directory for the application under the name of the controller method.

Note, though that we can also use render and redirect methods in our controller to influence or change this behaviour.


This is one of a series of summaries taking you from initial installation of Ruby and Rails through to a complete multitable application:
[link] - Installing Ruby and Rails
[link] - Hello World, Ruby on Rails Style
[link] - What and Why - Model, View, Controller
[link] - Multiple views, same model
[link] - A form to allow data to be added to the model
[link] - Validating data entries for storage in the model
[link] - Cleanly viewing model data
[link] - Complete sample, including a multiple table model
These topics are covered on our Introduction to Rails day - an optional extension of Ruby Programming or Learning to program in Ruby