Chapter 5. Building the User Interface

In the last chapter, we worked on implementing a solid model layer for the application. While the console and unit tests are a fine way for developers to access the models, they are not a suitable interface for the intended users of the system (Acme administrative staff). This chapter describes how to build a web interface onto the models we have developed, including:

  • A walk-through of creating a controller from scratch
  • How to use views, layouts, and helpers
  • Using pagination
  • Linking views together to create drill-downs
  • Adding style-sheets
  • Writing custom helpers and partials
  • Writing complex controller actions to update multiple models simultaneously
  • Fleshing out the application functionality

By the end of the chapter, we will have a basic, fully-functional web-based CRM application.

Controllers and Views: A Recap

The model layer of the last chapter is the first part of the Model-View-Controller jigsaw. Implementing a user interface on top of the model layer means building the two remaining MVC components (see the section Model-View-Controller Architecture at the start of Chapter 4):

  1. Controllers, which handle the control flow and interact with the model layer
  2. Views, which present data to the user and enable them to interact with the controllers

How do you decide which controllers and views do you need to build? Rails helps us with our decision making by suggesting a convention: one controller for each model. The controller for the model handles all of the operations on it; typically the CRUD operations, i.e.

  • Creating a new instance
  • Retrieving all the instances of the model
  • Updating an instance
  • Deleting an instance

Making a controller for each model seems a sensible place to start. In the rest of the chapter, we will build up the following controllers, corresponding to the models we built in the previous chapter:

Controller class

File containing controller definition (in app/controllers)

PeopleController

people_controller.rb

CompaniesController

companies_controller.rb

AddressesController

addresses_controller.rb

Notice how we are applying the conventions mentioned in Chapter 4 (Rails and MVC): the controller name is the name of the model, pluralized, with "Controller" appended. The file name is the lowercase version of the controller class name, with underscores separating discrete words.

Note

There is nothing to stop you having a single controller, which manages all of your tables. However, this would make the class definition too complex. By sticking to the conventions, we get a neat segregation of the actions and views specific to a particular model.

We'll be looking at the workflow for building a simple controller, explaining where to put the code that defines the controller actions and where to put the associated views. Along the way, we'll see how to organize the layout for your application and customize the look and feel with a stylesheet.

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

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