Week 4

Assignment: create a poetry website

In this assignment you'll create your first web application. It will list a collection of poems, and users will be able to click through to view the poem's text.


Create a new Rails project called poetry. Change to the new poetry directory by typing cd poetry.

Start ( git init) a new git repository for the poetry, add all the new files (git add .) and commit everything so far ( git commit -m "Your message").

Create the Poem model

Create the database with bin/rake db:create

Generate a model called poem. In its migration, add these fields:

  • title (string)
  • author (string)
  • body (text)
  • source (string)
  • published (boolean)

Run the migration (bin/rake db:migrate)

Commit your changes with git (git add . and then git commit -m "Your message").

Add demo data

Copy the contents of my demo data file into db/seeds.rb (you can delete everything else that is there). Run bin/rake db:seed to load the demo poems into your app.

Use bin/rails console and make sure your Poem model can has some poems stored in the database (e.g. Poem.all, Poem.count, Poem.first)

Commit your changes with git.

Create the Poems controller

Generate a Poems controller. Add two methods: index and show. Create the appropriate routes in the routes file. (You can check that the routes are working with bin/rake routes.)

The index method should set an instance variable called @poems that contains all the published posts. You'll need to use one of the model query methods to only return poems where published is true (see the resources linked below).

The show method should set an instance variable called @poem that looks up a single poem with params[:id].

Create the matching HTML templates

app/views/poems/index.html.erb should loop through each poem in @poems. It should print out each poem's title and author, and link to the poem's page on the site you're building.

app/views/poems/show.html.erb should print out the specified poem's title, author and text. It should also link to the poem's source link.

The show view template should also link back to the index page. When you print out the body of the poem, use the simple_format method to add BR tags.

Commit your changes with git. Then push your changes to Github (git push origin master).

Bonus: add a bit of CSS

Feel free to add some CSS so things aren't quite so grim looking. You can add CSS to app/assets/stylesheets/application.css (something like this) if you'd like.

Submit your work

Email me a link to your repository on Github: alex@dunae.ca


IMG 240: Git basics

IMG 240: Creating a Rails application

IMG 240: Blog Rails application

Poems for your seeds.rb file

Rails Command Line Reference (generator commands)

Rails ActiveRecord Querying Reference (different methods for querying your model)

Rails Routing Reference (how to setup your routes file)

Rails ActionView Helpers (scroll down and click through to explore UrlHelper, TextHelper, NumberHelper and anything else that catches your eye - you can use any of these functions in your HTML template files)

Reading for next week

Getting Started with Rails, Chapter 5

Rails Controllers (parts 1 through 4)