- Rhomobile Beginner's Guide
- Abhishek Nalwaya
- 318字
- 2025-03-31 04:29:55
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:
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:
- Create an action
employee_details
inemployee_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. - Create a page
employee_details.erb
andemployee_details.bb.erb
in theEmployee
directory and add the following lines toemployee_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>
- 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.
- Create a file
employee_helper.rb
in the helpers folder. - 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
- 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>
- 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
- 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.