Homework 2

Overview

The goal of this assignment is to build a web application using your database. The term "Web 2.0" refers to a set of interactive web applications that focus on user-submitted content and social interaction. You will build a simple Web 2.0 application that will allow users to tag cities in your database, and link to a map of the city.

We will use Ruby on Rails to build the application. You will likely need to use online documentation and/or an online Ruby on Rails tutorial to help you figure out how to do parts of this assignment.

Update

We have received a fair deal of questions regarding the use of migrations for this assignment. However, as it says in the assignment itself, you are NOT to use migrations for the final version you turn in. We realize that migrations is one possible way of doing this, but part of the assignment is adhering to the original set of constraints set out. For those who have had some difficulty with this, the message board has seen numerous hints posted (such as creating an id column, or explicitly specifying which field will be the primary key in rails), which we believe aid in the completion of this project.

Due

This assignment is due April 10, but you should start early. We do not necesarily expect this assignment to be conceptually difficult, but it may be tricky to work in a new programming environment and programming language so give yourself plenty of time.

1. Preparing your database

You can use some of the data and database scripts you set up for the first assignment. However, it must conform to the rails naming convention. For example, if you are going to create an application named "factbook" then your database should be called "factbook_development". If you have a "City" object then the table of cities should be the plural, that is, "cities." Create the appropriate database and load your data into Postgres.

Note this means you will not have to use rails migrations and fixtures. You can play with them if you would like to learn how they work, but the assignment you turn in should load postgres directly, not via migrations and fixtures (just to make grading consistent and easy for us.) Migrations and fixtures are meant to make your life easier by avoiding all the work you did in assignment 1; but since you have already done that work it is okay to just create and load your tables directly.

2. Setting up a Rails application

The "ruby" and "rails" executables are available at:

/home/ff/cs186/pkg/ruby-1.8.5-i86pc/bin/
Make sure you are running on a Solaris x86 machine, such as rhombus (run the 'arch' command to make sure).

First, choose a name for your application (like "factbook") and use rails to create a new application with that name:

% rails -d postgresql factbook
This will set up a skeleton application that does nothing. You can verify that your rails application is properly set up by using these steps:
  1. Find your $WEBPORT:
    % echo $WEBPORT
    
    This will be a number like "15999".

  2. Change to the project directory you just created:
    % cd factbook
    
  3. Run the embedded rails server:
    % script/server -p $WEBPORT
    
    This will start a web server listening on your web port.

  4. Point your browser at your server, for example http://rhombus.cs.berkeley.edu:15999/, using your $WEBPORT number instead of 15999. If you have successfully set up your application, you will get:

    Welcome aboard
    You're riding the Rails!

3. Implementing your application

You will need to create a rails application that has the following features:

4. Some hints

Rails is based on a Model-View-Controller pattern. The model is a representation of the persistent data, while the view is what the external user sees. The controller processes the data to help populate the view. For example, the controller can contain SQL queries that set variables which are then displayed in the view (this is done with the ActiveRecord find_by_sql method). The ActiveRecord class in rails includes a "find" method for avoiding SQL; it will be up to you to find a way to retrieve the appropriate data, whether from find_by_sql or find. You will also likely find ActiveRecord.new and ActiveRecord.save helpful.

Rails will do many things for you. For example, to create a new model named "City", change to your application directory (e.g. "factbook") and type:

% script/generate model City
Similarly, you can generate controllers and views. Run
% script/generate
to see command line arguments, and
% script/generate controller
to see arguments specific to generating a controller (this also works for models and views.)

For this assignment, you do not need to run "rake". Rake is like "make" and automates many things; however we are building a relatively simple application. Also, you will be able to complete this assignment by editing the controllers and views, but you shouldn't have to edit the models. (You will potentially also have to edit config files like "database.yml".)

5. Deliverables

You need to turn in two things: