Week 5 "Extinctions" Assignment
We've got an uplifting project this week, web app scientists :) You'll be creating a database of animals from North and South America that went exctinct during the holecene extinction (past 12,000 years to present).
Extinctions: seeds.rb
(save to db/seeds.rb)
Extinctions: holocene-americas.csv
(save to db/holocene-americas.csv)
Setting up the data
- Create a new Rails app called
extinctions - Initialize a git repository (in the
extinctions) directory and commit all the files (a good commit message would be something like "Setup Rails project") -
Generate a model called
Classificationwith the following fields in its migration:- name (string)
-
Generate a model called
Animalwith the following fields in its migration:- classification (references)
- name (string)
- range (string)
- url (string)
- image_url (string)
- extinction_date (integer)
-
Add a
recent?instance method to theAnimalmodel (class). It should returntrueifextinction_dateis after 1900, andfalseotherwise. -
Generate a model called
CommonNamewith the following fields in its migration:- animal (references)
- name (string)
-
Add the associations to the files in the
app/modelsdirectory (bothbelongs_toandhas_many)- Classification has many Animals, Animals have many CommonNames
- Add your files and commit with git (a good commit message would be something like "Added 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.rbanddb/holecene-americas.csv(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 Classifications controller and views
-
Generate a
Classificationscontroller and add it to yourroutes.rbfile- Add an
indexmethod that retrieves the list ofClassificationrecords -
Add an
indexview that displays:- each
Classificationname - with the number of animals that are included (e.g. "Mammalia (10 animals)")
- a link to the
ClassificationsController#showmethod
- each
- Add a
showmethod and view -
The
showview should display- the current
Classificationname at the top of the page - links to each animal – these links should point to the
AnimalsController#showroute - the animals should be sorted by exctinction date, oldest to newest (do this in the controller)
- a picture of each animal (but only if it has one) – you'll want to use the
image_tagmethod - whether the extinction was recent (use your
recent?method)
- the current
- Add an
- Add a
rootroute that points to theClassificationsindex view - Add your files and commit with git (a good commit message would be something like "Added Classifications controller and views")
Setting up the Animals controller and views
-
Generate an
Animalscontroller and add it to yourroutes.rbfile- Add a
showmethod and view file to display an individual animal -
On the animal's
showpage display- the animal's scientific name
- the animal's common names (if any)
- display its image (but only if it has one) – you'll want to use the
image_tagmethod - whether the extinction was recent (use your
recent?method) - link to the source URL
- Add a
- Add your files and commit with git (a good commit message would be something like "Added Animals controller and views")
Optional: nicer URLs
- Figure out what Rails'
to_parammethod does an use it to include record names in your URLs - Read up on nested routes and figure out how to nest the
:animalsresource under the:classificationsresource
If you do both of these things, you should be able to have URLs like /classifications/1-mammalia/animals/123-bison-priscus
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