You should already have installed Ruby on Rails for Project #2. If not, follow the installation instructions now. Once Rails is installed, create a Rails application for this project by invoking the following command:
rails new project3
This will create a directory project3
that contains a
skeleton for the project. Your solutions for all of the problems
below should be implemented in this project. Change to that directory
and invoke the following command:
rails server
This will start a Web server in this project, and the server's log information will appear on your console. Now go to your Web browser and enter the following URL:
localhost:3000
This should display a default root page provided by Ruby on Rails. You can type control-C to the server to kill it.
You will need to modify the default Rails configuration in two ways for
this class. First, enable traditional ("non RESTful") routes by editing
the file project3/config/routes.rb
.
Insert the following lines just before the final end
statement:
get ':controller(/:action(/:id))' post ':controller(/:action(/:id))'
Second, modify the session configuration so that sessions are stored
in the database rather than in cookies (you will learn about sessions
later in the class). To do this, edit the file
project3/config/initializers/session_store.rb
.
The last line should look like this:
Project3::Application.config.session_store :cookie_store, key: '_project3_session'
Comment out this line by adding a '#' at the start, then insert additional lines so that the end of the file now looks like this:
# Project3::Application.config.session_store :cookie_store, key: '_project3_session' # Use the database for sessions instead of the cookie-based default, # which shouldn't be used to store highly confidential information # (create the session table with "rails generate session_migration") Project3::Application.config.session_store :active_record_store
Then edit the file project3/Gemfile
, adding the
following lines at the end:
# Enable ActiveRecord session store gem 'activerecord-session_store'
Finally, invoke the following shell commands in the project3
directory:
bundle update rails generate active_record:session_migration rake db:migrate
Create a model, view, and controller in the project3
application, which together will display the names of all states
containing a given substring. Your code must implement the URL
/state/filter
, which accepts a query value named
substring
. The URL should produce a
Web page that lists in alphabetical order all states whose names
contain the given substring
(ignoring differences in case). For example, the Web page for
/state/filter?substring=al
should list the states
Alaska, Alabama, and California. The page should also display the
substring that was used to filter the states. If there are no
matching states then the Web page should display a message indicating
that fact (rather than just showing nothing).
First, create a simple model that will manage the data. For this assignment
you will not use the full-fledged Rails models discussed in lecture (you
will use "real" Rails models for all project starting with Project #4).
All you need to implement is a class named
State
with a single class method State.filter
(a class method is one that can be invoked without an instance of the
class, like a static method in Java). State.filter
should
take a single string argument and return an array containing the names
of all states whose names include the argument as a substring (ignoring
differences in case). To save typing you can download
states.rb,
which contains Ruby code to create an array containing the names
of all of the states. You can copy this code into your application.
Once you have defined the model you can then create the controller
and view. Follow the Rails conventions for file and method naming
and for where to put various functionality. Note that Rails provided
a default layout in
project3/app/views/layouts/application.html.erb
, which
handles everything except the contents of the <body>
element. Replace the DOCTYPE
and html
lines
at the top of this file with the standard header lines for this class.
Modify the layout so that it displays a personalized
header at the top of the page (the layout will now include some HTML
inside the <body>
element). Use your imagination
and creativity to
create a header that is "uniquely you". This can include images,
graphics, whatever you like. Be creative! From now on, use this
layout for all of your Rails projects in the class. Each individual
page should be able to specify its own title, which gets used by the
default layout.
Define a new controller and view that implement the URL
/tabs/show2
.
This URL must display a page containing
two sets of tabs separated by a half-inch or so of white space.
The first set must be exactly the same as your
solution to Problem 2 in Project 1. The second set must have the
same general appearance but must display different labels (which
you can choose). You must use a partial-page template named tabs
to generate the tabs; your view should invoke it twice. The partial-page
template takes two parameters. The first is an array of hashes,
one for each tab; each hash has a :label
element containing
the text to display in the tab, and a :url
element containing
the URL to visit when that tab is clicked. The second parameter for the
partial-page template is a string containing the label of the selected
tab; the partial-page template uses it to identify the selected tab and
display it differently.
These points will be awarded if your problem solutions have proper MVC decomposition, follow the Rails conventions, and generate valid XHTML. In addition, your code and templates must be clean and readable. The last 5 style points will be reserved for layouts that are unusually interesting (only a few projects will receive these points).
cd
to the directory
containing the application and invoke the following command:
rails server
project3/assets/images
. Second, use the Rails method
image_path
to generate image names. If you need to
reference an image in a CSS file, give the CSS file an extension
.css.erb
. This allows the file to contain embedded Ruby
just like HTML templates. Then, where you would previously have
used a construct like url(left.gif)
, use this construct instead:
url(<%= image_path("left.gif") %>)
image_path
will return a URL that maps to
the image in project3/app/assets/images/left.gif
(Rails will manage all the details). To reference images in your
HTML templates, don't use <img>
directly; instead, use the image_tag
mtehod.
application.html.erb
file contains constructs
related to Turbolinks (which we don't use in this class) that do not
validate. To make your pages validate, modify
application.html.erb
to remove the
javascript_include_tag
line (not needed) and remove
"data-turbolinks-track" => true
from the preceding line.
Use the standard class submission mechanism
to submit the entire application (everything in the project3
directory). Note: all of your generated HTML must validate as
proper XHTML (you do not need to validate your CSS for this project).