Week 6 "Personal Library" Assignment
This week we'll be creating a "Personal Library" web app, where you can keep track of things you've read or watched, as well as things you'd like to read or watch.
The app will just have one controller that will list items in your library. It will allow users to add, modify and delete items from the library.
Even better, it will use Google Image search to grab an image for each item in the library.
Library: seeds.rb
(save to db/seeds.rb
)
Library: image_fetcher.rb
(save to app/services/image_fetcher.rb
)
Setting up the data
- Create a new Rails app called
library
- Initialize a git repository (in the
library
) directory and commit all the files (a good commit message would be something like "Setup Rails project") -
Generate a model called
ItemType
with the following fields in its migration:- name (string)
- OPTIONAL (WE DID NOT COVER THIS IN CLASS): Add a validation in the model to make sure that
name
is always present -
Generate a model called
Item
with the following fields in its migration:- item_type (references)
- title (string)
- image_url
- completed_on (date), default should be
nil
- this is the date the item was read or watched
- OPTIONAL (WE DID NOT COVER THIS IN CLASS): Add a validation in the model to make sure that
title
is always present -
Add a
completed?
instance method to theItem
model (class). It should returntrue
ifcompleted_on
is NOTnil
, andfalse
otherwise. -
Add the associations to the files in the
app/models
directory (bothbelongs_to
andhas_many
)- ItemType has many Items (don't forget to add the reverse assocation as well)
- Add your files and commit with git (a good commit message would be something like "Added Item and ItemType models")
-
Run the migrations
(If you get messed up at this point you can clear everything in the database withbin/rake db:drop db:create db:migrate
) -
Copy in seed data in
db/seeds.rb
(linked above) and load the seeds (bin/rake db:seed
) - Add your files and commit with git (a good commit message would be something like "Added seed data")
Setting up the Items
controller and views
- Generate a
Items
controller - Add the resource entry to your
routes.rb
file (also make theroot
route point toitems#index
) - Make an
index
method that retrieves the list ofItem
records -
Add an
index
view that displays:- each
Item
title, itsitem_type.name
, its image, and whether it has been "completed" (use thecompleted?
method)
- each
- Add your files and commit with git (a good commit message would be something like "Added Items controller and index view")
Adding the forms to the Items
controller
-
Add the
new
andedit
actions to the controller – see this Github link for an example -
Add the
new
,edit
and_form
views – see this Github link for examples -
Make sure your form allows editing of the following fields:
- title (
text_field
) - item_type (
collection_select
) - completed_on (
date_select
)
- title (
-
Add the
create
,update
anddestroy
controller methods – see this Github link for examples - Add a link on your index view to edit and destroy each entry – see this Github link for examples
- Add your files and commit with git (a good commit message would be something like "Added item editing views")
Time for images
-
Copy
this ImageFetcher class
into a new file, called
app/services/image_fetcher.rb
- Modify your controller so it will fetch get an image URL for each item when it is saved (so after creating and updating). There are lots of different ways to do this. See if you can find one.
- Add your files and commit with git (a good commit message would be something like "Wired up ImageFetcher")
Bonus: add a mark_as_complete
route
We're not limited to just show/create/update/destroy – it's possible to add any other actions you need.
If you want to try this bonus challenge, read about
Adding more RESTful actions on the Routing guides.
You'll have to add the mark_as_complete
route in your routes file and your controller, plus link to it from a view.
Submit your work
When you are done, push your work to Github. Then email me a link to your repository on Github: alex@dunae.ca