Você está na página 1de 4

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:
2.
3. % echo $WEBPORT

This will be a number like "15999".


4. Change to the project directory you just created:
5.
6. % cd factbook

7. Run the embedded rails server:


8.
9. % script/server -p $WEBPORT

This will start a web server listening on your web port.


10. 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:

Each city in the database will be associated with a latitude, a longitude, and a set of
tags. Each tag is a word or phrase that describes a city. For example, the city
"Moscow" might have tags "kremlin" and "russia".
You should provide a view, called "viewcity", that allows you to see the name of the
city, and the current set of tags for the city. In this view, you should also be able to
add a tag, and after adding a tag the new tag should appear in the view alongside the
existing tags.
Your viewcity view should also allow you to retrieve a map of the city. You can use
Yahoo! maps or any competing map service (Google is acceptable I suppose). Here is
a snippet of javascript that you can embed in your view to place the city on a Yahoo!
map.
You should provide a view, called "search", that allows you to search for cities by
name or tag. That is, if the user enters a tag, you should display a list of the cities that
have that tag. If the user enters a city, you should display the city with that name. In

either case, clicking on the name of a city in the search result should take you to the
viewcity view for that city. Each city should only appear once in the search results,
although if there are different cities with the same name (like Paris, France and Paris,
Nevada) both should appear. The same search box should be used whether you are
searching by tag or by city name. This should work even if the name of the city is not
also a tag for the city.
Clicking on one tag in the list of tags in the viewcity view for a city should display a
list of all the cities tagged with that tag, just as if you had searched for that tag in the
search view.
The viewcity view should also show the ten "nearest" cities to the viewed city. You
can compute the distance between two cities treating longitude and latitude as points
on a grid, and finding the Euclidean distance. Clicking on any of the nearest cities
should take you to the viewcity view for that city.
You should provide a view, called "country", which allows you to view attributes of a
country. These attributes are things like GDP, population, etc. that you extracted for
homework 1. Your country view should allow you to edit the values of these
attributes, and clicking a "Save" button will save your changes to the database. After
clicking save, the edits to the attributes should be visible on the country view.
The country view should also list all the cities that are in the country, and clicking on
the name of a city should take you to the viewcity view for that city. In the viewcity
view for the city, the name of the country should be displayed, and clicking on the
name of the country should take you to the country view for that country.

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:

A single file, called createdb.sql, that creates the necessary database and tables in
postgres, and populates them with some sample data.
A tar file of the app/ directory of your rails application (e.g. factbook/app). This can
be produced as follows:
% cd factbook
% tar cvf app.tar app/

Você também pode gostar