Creation of a new page

We will now add a new page manually in our employee application, which will list all the employee names and their respective designations.

Let us include following use-cases to make our application more interesting:

  • If age < 30 then, designation – Developer
  • If age >= 30 && age <40 then, designation – Senior Developer
  • If age >= 40 then, designation – Architect

Time for action – Creation of the new page

We will now create a new page (employee_details.erb) that will display the employee name and designation.

Follow these steps to create the page:

  1. Create an action employee_details in employee_controller. We write all the database queries using Rhom, which is an ORM of Rhodes:
     def employee_details
         @employee = Employee.find(:all)
      end 

    By adding Employee.find(:all) we will return an array of an object from the Employee Model.

  2. Create a page employee_details.erb and employee_details.bb.erb in the Employee directory and add the following lines to employee_details.erb:
    <div class="pageTitle">
      <h1>Employees Details</h1>
    </div>
    
    <div class="toolbar">
      <div class="leftItem regularButton">
        <a href="<%= Rho::RhoConfig.start_path %>">Home</a>
      </div>
      <div class="rightItem regularButton">
        <a class="button" href="<%= url_for :action => :new %>">New</a>
      </div>
    </div>
    
    <div class="content">
      <ul>
        <% @employees.each do |employee| %>
        
            <li>
              <a href="<%= url_for :action => :show, :id => employee.object %>">
                <span class="title"><%= employee.name %> is <%= find_desigation(employee.age.to_i)%></span><span class="disclosure_indicator"></span>
              </a>
            </li>
        
        <% end %>
      </ul>
    </div>
  3. And, add these lines to employee_details.bb.erb:
    <div id="pageTitle">
       <h1>Employees Details</h1>
    </div>
    
    
    <div id="toolbar">
        <%= link_to "New", :action => :new %>
        <%= link_to "Home", RhoConfig::start_path %>
    </div>
    
    <div id="content">
      <table>
        <tr>
        <% @employees.each do |obj| %>
            
                <td class="recordLabel"><%= link_to "#{obj.name} is #{find_desigation(obj.age.to_i)}  ", :action => :show, :id => obj.object %></td>
            
        <% end %>
        </tr>
      </table>
    </div>

    We have used a helper method find_designation, which returns the designation of that employee. This helper is created in the next step.

  4. Create a file employee_helper.rb in the helpers folder.
  5. Include the employee_helper in controller so that it can be accessed in our view:
    require 'helpers/employee_helper'
    include EmployeeHelper
    Add the logic in employee_helper.rb which we created in the last section
    module EmployeeHelper
      def find_desigation age
        if age < 30=
          return "Developer"
        elsif age >= 30 and age < 40
          return "Senior Developer"
        else
          return " Architect"
        end
      end
    end
  6. Edit index.erb file in app folder to add a link:
            <li>
          <a href="<%= url_for :controller => :Employee, :action => :employee_details %>"><span class="title"> Employee Details</span><span class="disclosure_indicator"/></a>
        </li>
  7. Edit index.bb.erb in the app folder and add a link to our page for Blackberry:
    <tr>
            <td class="recordLabel">  <%= link_to "Employee Details", :controller => :Employee, :action => :employee_details%>
    </td>

    Now, build this code for iPhone as explained in this chapter earlier:

    $rake run:iphone
  8. And similarly for Blackberry by:
    $rake run:bb

We just created a new page in which the designation of employees are calculated on the basis of their age. We have also learnt how to write a helper method in our application.