2503ICT: Rails 1 - Basic Concepts

Under construction!


Three-tier Web architecture pattern:

(discussed previously)

Model-view-controller pattern:


The Rails version of the MVC pattern:

| HTTP request with URL incl. parameters and data
Router (that connects URL patterns to controller actions)
| HTTP parameters and data 
Controller action (that implements application logic)
1. Model to retrieve and update data using OO API <-> SQL database
2. View to populate an HTML template
Redirect to Router 
HTML page to Client

Common Rails tasks

Create a scaffold

See yesterday's example, Chapter 2 of Hartl's tutorial.

Most useful for administration services.

Define a model

A model is a generalisation of a Rails class (cf. a Java class) or a relational database table. (But sometimes the software engineering literature talks as if a model is the whole database.)

rails generate model User name:string email:string

This creates two main files

To perform one or more defined migrations, give the command:

rake db:migrate

We can explore models and tables using the following three (Linux) commands, each of which starts an interactive session.

rails console
rails dbconsole
sqlite3 db/development.sqlite3

The first command allows us to create, find, update and destroy objects (and table rows) using an object-oriented interface. A few common commands are these:

user = User.new(name: "Rodney", email: "rwt@gu.edu.au").save
user = User.create(name: "Rodney", email: "rwt@gu.edu.au")
user = User.find(1) # finds user with id=1
user = User.find_by(name: "Rodney")
users = User.all
users = User.where("name = ?", "Rodney")
user.update(name: "Dave")

The second and third commands allow us to perform the same operations on table rows using SQL.

It's worth experimenting at this point with each of these commands, creating, finding, updating and destroying ojects.

Define a controller

A controller for a set of similar objects consists of actions (or method definitions) for performing one or more common, related tasks. These tasks are normally selected from the following specific set (though this is not the case for the static_pages controller),

The name of a new controller is normally associated with a previously defined model. When we create the controller, we specify the actions to be provided.

rails generate controller users new create index show

(Note that the name of the controller is always plural!)

This command creates a file app/controllers/users_controller.rb with empty definitions for the actions new, create, index and show.

It also create two HTML templates app/views/users/new.html.erb, .../create.html.erb, .../index.html.erb and .../show.html.erb. (This seems pretty silly, actually, as we don't need the file create.html.erb, so I may be misunderstanding something here.)

Define routes

Routes determine which HTTP requests are handled by which actions depending on the URL in the request and the HTTP method in the request.

By default, the controller action above, establishes the following routes:

It is also possible to establish routes explicitly in the file config/routes using the function root and get, as explored in Laboratory 1.

When you establish a route using the function get in file config/routes, you can also define a "path" that can be used in HTML templates, again as explored in Laboratory 1.

Define resources

Instances of a model (or class) are called resources. You can also specify a set of resources and the actions that can be used to operate on them in the file config/routes.rb.

For example, if Category and Item are models, we could add the following two lines at the start of /config/routes.rb:

resources :categories, only: [:index, :show]
resources :items

For the model Category, this defines routes /categories to "categories_controller#index" and categories/n to "categories_controller#show". It also created two empty actions index and show in the file .../categories_controller.rb and two HTML templates .../index.html.erb and .../show.html.erb.

For the model Item, this defines seven routes (one for each of the standard actions), and five HTML templates (or views), omitting views for actions create and update (which don't return HTML pages to the client). I'll explain this later.

Define views and 'partials'

Under construction...

RESTful Web Design

Rails supports the principle of RESTful Web design. This principle is described in more detail later. For now, it requires that every URL denotes a "resource" (a user, a list of users, etc.) and that the action to be performed on the resource (create, update, show) is specified in the HTTP request (and not as part of the URL). The design of the URLs is thus related to the data modelling in the application.

Example: Guestbook app

See links on Examples page.