Story 2 – Create new and List existing Games

Story 2 – Add a New Game and List existing Games

Support the New Football Game and Games pages we got from our product owner as shown on http://akikijuurails.com/football-predictor-project/

A game should have home_team, away_team, home_score, away_score

Remember

  • To work in the football project we started last time (in DOS cd c:\workshop\football)
  • If you have trouble look back on Story 1 we did last time

Task 1 – Create a Model and Database Table for Games

Hints:

  • ruby script/generate migration _____________   (fill in the  blank yourself)
  • edit the migration
  • rake db:migrate
  • write a test similar to
  • run “rake test” and make sure it passes

Remember:

  • Do not run ruby script/generate scaffold
  • Write a test that shows you can load and save games in the database

Task 2 – Create a controller and view to display all Games

Hints

  • Create a new controller called games_controller.rb
  • Add “map.resources :games” to your config/routes.rb
  • Add an index action
  • Add a games/index.html.erb view
  • Use ruby script/console to save 2 games into the database
  • Test that http://localhost:3000/games shows those 2 games

Task 3 – Be able to get to a new game page with a form

We are done when you can

  • Go to http://localhost:3000/games
  • Click on create new game
  • See a page with a form where you could enter the information for a game (home_team, home_score, away_score, away_team)

Hint

  • Add an new action
  • Add a games/new.html.erb view
  • Use form helpers in your view similar to

Task 4 – Be able to save our new game

We are done when you can

  • Go to http://localhost:3000/games
  • Click on create new game
  • Fill out the form
  • Click submit
  • See the new game on the list all games page
Hint
  • Add a create action to your controller
  • Add code similar to

Task 5 – Refactor: DRY up your view

You now likely have two forms, one for creating a new game and one to edit a game. Remember we don’t want code that does the same thing more then once. Use a partial for the form html. Let’s call this new file _form.html.erb.

Hint: Use RailsGuides (http://guides.rubyonrails.org) to help you learn how to use partials. Read the following sections in the layouts guide:

  • 3.4 Using Partials

    • 3.4.1 Naming Partials

    • 3.4.2 Using Partials to Simplify Views

    • 3.4.4 Passing Local Variables