Adding a Controller for Companies

The previous sections went into some detail about the concepts of controllers and views, and how they fit together. In this section, we will fast-track the creation of another controller and a view (for companies), both to get a feel for the flow of creating a controller, and to summarize what we've just covered.

Create the CompaniesController

First, generate the controller for the Company model from the command line:

$ script/generate controller companies

Now, add the index action (which will display a list of companies) to app/controllers/companies_controller.rb:

class CompaniesController < ApplicationController
def index
@paginator, @companies = paginate :company, :per_page => 10,
:order => 'name'
@page_title = "List of Companies"
end
end

Create the Index View

Add the view for the index action (in app/views/companies/index.rhtml):

<h1><%= @page_title %></h1>
<table>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Fax</th>
<th>Website</th>
</tr>
<% for company in @companies -%>
<tr>
<td><%= company.name %></td>
<td><%= company.telephone %></td>
<td><%= company.fax %></td>
<td><%= link_to(company.website, company.website) %></td>
</tr>
<% end -%>
</table>
<p>
<% page_num = @paginator.current.number -%>
<% last_page_num = @paginator.last.number -%>
<%= link_to('Previous', :page => page_num - 1) + "&nbsp;" unless 1 == page_num -%>
<%= pagination_links(@paginator) %>
<%= link_to('Next', :page => page_num + 1) unless last_page_num == page_num -%>
</p>

Note that the pagination code here is identical to the code in the people index template. We will investigate a way to avoid this repetition later in the chapter (see the section Rendering Pagination Links with a Partial).

Because this view contains almost all the information relating to a company (barring the company address, which we'll return to later in the section Showing Associated Records), a view to show a single company is redundant, as it will contain no more information than the index view. For now, then, we can leave this action out. There is an important point here: if you don't need an action, don't add it to the controller. This is also an advantage of custom-building your actions and views, as opposed to using the scaffold to generate them for you.

Test It!

That's all the work we need to do to view the list of companies. Fire up the server and browse to: http://localhost:3000/companies. You should see the following page:

Test It!

You may notice that there are gaps in some of the cells in the table, if a company doesn't have a particular attribute specified (e.g. missing fax or telephone number). We will deal with this situation later (see the section Default Messages for Empty Fields later in this chapter).

Summary

This section demonstrated the whole process of writing controllers and their views, showing how to build the CompaniesController to display data from the Company model. With a firm grounding in the concepts underlying controllers, views, layouts, and helpers, we are well prepared for building more complex actions and views, including those for managing data input and validation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.133