CS142 Project #5: Forms, Sessions, and Validation

In this project you will extend your work on Project #4 by adding forms for logging in, commenting on existing photos, and uploading new photos. In order to implement these new features you will need to use the Rails facilities for sessions and validation. To get started, copy the directory tree for Project #4 to a new directory named project5. Do all of your work for this project in the new directory.

Problem 1: Simple Login (10 points)

Write a database migration that will add a new attribute login to the User model. This attribute is a string containing the identifier the user will type when logging in (their "login name"). Include code in your migration to initialize the login attribute for each existing user to the users's last name, converted to lower case.

Create a new controller that implements 3 new URLs:

In addition to implementing these URLs, include support for login/logout in the standard layout used for all of your application's pages. If there is no user logged in, the banner at the top of each page should include a small "Login" link to the login page. If there is a user logged in, the banner should include a small message "Hi Alice! Logout", where "Alice" is the first name of the logged-in user and "Logout" is a link to the logout page.

Problem 2: New Comments (10 points)

Once you have implemented user login, the next step is to implement a form for adding comments to existing photos. Implement a URL /pics/comment/id that displays a form where a user can add a comment for the photo whose primary key is id. You should also display the photo on this page so the user can see it while he/she is typing the comment. The form should post to the URL /pics/post_comment/id; your implementation for this URL should create a new comment in the database using the Rails models. The comment must include the identifier of the logged in user and the time when the comment was created. Make sure that new comments can be viewed in the same way as pre-existing comments.

Once you've implemented the form for new comments, modify the page /pics/user/id to display a "New Comment" link next to each photo, which will go to the new-comment form for that photo.

Your implementation must handle the following errors:

Problem 3: Photo Uploading (10 points)

Allow users to add new photos. To do this, implement a URL /pics/photo, which displays a form where the user can select a photo file for upload. The form should post to the URL /pics/post_photo, which copies the incoming photo data to a file in the directory project5/public/images and creates a new record in the database containing the name of the photo file, the creation time, and the identifier of the user. Also, add a "New Photo" link at an appropriate place in one of your existing pages, which users can click to go to the photo upload form.

Your implementation should check to make sure that a user is logged in and prevent photo uploading if not.

Problem 4: Registration and Passwords (10 points)

Enhance the login mechanism with support for new-user registration and passwords, using a "salting" mechanism like that described in the Rails book starting on page 153:



Use the standard class submission mechanism to submit the entire application (everything in the project5 directory). Please indicate in a README file whether you developed on Windows or a Macintosh (we may need this information in order to test your solution). Note: If you have added more than a couple of new images, please clean up your data before submitting, as described above. Be sure to test your project one more time after resetting the database, just to make sure everything is still OK.