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
Classification
with the following fields in its migration:- name (string)
-
Generate a model called
Animal
with 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 theAnimal
model (class). It should returntrue
ifextinction_date
is after 1900, andfalse
otherwise. -
Generate a model called
CommonName
with the following fields in its migration:- animal (references)
- name (string)
-
Add the associations to the files in the
app/models
directory (bothbelongs_to
andhas_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.rb
anddb/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
Classifications
controller and add it to yourroutes.rb
file- Add an
index
method that retrieves the list ofClassification
records -
Add an
index
view that displays:- each
Classification
name - with the number of animals that are included (e.g. "Mammalia (10 animals)")
- a link to the
ClassificationsController#show
method
- each
- Add a
show
method and view -
The
show
view should display- the current
Classification
name at the top of the page - links to each animal – these links should point to the
AnimalsController#show
route - 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_tag
method - whether the extinction was recent (use your
recent?
method)
- the current
- Add an
- Add a
root
route that points to theClassifications
index 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
Animals
controller and add it to yourroutes.rb
file- Add a
show
method and view file to display an individual animal -
On the animal's
show
page 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_tag
method - 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_param
method does an use it to include record names in your URLs - Read up on nested routes and figure out how to nest the
:animals
resource under the:classifications
resource
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