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

  1. Create a new Rails app called library
  2. Initialize a git repository (in the library) directory and commit all the files (a good commit message would be something like "Setup Rails project")
  3. Generate a model called ItemType with the following fields in its migration:
    • name (string)
  4. OPTIONAL (WE DID NOT COVER THIS IN CLASS): Add a validation in the model to make sure that name is always present
  5. 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
  6. OPTIONAL (WE DID NOT COVER THIS IN CLASS): Add a validation in the model to make sure that title is always present
  7. Add a completed? instance method to the Item model (class). It should return true if completed_on is NOT nil, and false otherwise.
  8. Add the associations to the files in the app/models directory (both belongs_to and has_many)
    • ItemType has many Items (don't forget to add the reverse assocation as well)
  9. Add your files and commit with git (a good commit message would be something like "Added Item and ItemType models")
  10. Run the migrations
    (If you get messed up at this point you can clear everything in the database with bin/rake db:drop db:create db:migrate)
  11. Copy in seed data in db/seeds.rb (linked above) and load the seeds (bin/rake db:seed)
  12. 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

  1. Generate a Items controller
  2. Add the resource entry to your routes.rb file (also make the root route point to items#index)
  3. Make an index method that retrieves the list of Item records
  4. Add an index view that displays:
    • each Item title, its item_type.name, its image, and whether it has been "completed" (use the completed? method)
  5. 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

  1. Add the new and edit actions to the controller – see this Github link for an example
  2. Add the new, edit and _form views – see this Github link for examples
  3. Make sure your form allows editing of the following fields:
    • title (text_field)
    • item_type (collection_select)
    • completed_on (date_select)
  4. Add the create, update and destroy controller methods – see this Github link for examples
  5. Add a link on your index view to edit and destroy each entry – see this Github link for examples
  6. Add your files and commit with git (a good commit message would be something like "Added item editing views")

Time for images

  1. Copy this ImageFetcher class into a new file, called app/services/image_fetcher.rb
  2. 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.
  3. 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