Web Apps - Dice - Solution

The problem

From class:

  1. A route for /dice in config/routes.rb.
  2. bin/rails generate controller dice, then an index action in it.
  3. An index view in app/views/dice/.
  4. In the view, roll two dice. Show both, and the total.

One die: rand(1..6). Challenge: the pictures, <img src="/images/dice/1.svg">. 1.svg to 6.svg are already in public/images/dice/.

Done? Check your page against your three sentences.

Before you built it, you wrote three sentences that would tell you the page works. Keep them next to you: the last section checks the page against them.

This is the tacos page again, with Ruby doing more of the work. Same three files, same errors, in the same order.

1. The route

In config/routes.rb, under the tacos line:

get("/dice", { :controller => "dice", :action => "index" })

Visit /dice: "uninitialized constant DiceController". Rails read the route and went looking for a controller called dice.

2. The controller

In the terminal that isn't running the server, with the name from the route:

bin/rails generate controller dice

It makes app/controllers/dice_controller.rb, an empty class, and the empty folder app/views/dice/. Refresh: "The action 'index' could not be found for DiceController". The route asked for index, and an action is a method. Add it:

class DiceController < ApplicationController
  def index
    render({ :template => "dice/index" })
  end
end

Refresh: "Missing template dice/index". One file to go.

3. The view, with numbers first

Make app/views/dice/index.html.erb, in the folder the generator made. Roll each die once, at the top, and keep the result in a variable:

<% die1 = rand(1..6) %>
<% die2 = rand(1..6) %>
<h1>Dice</h1>
<p>
  You rolled <%= die1 %> and <%= die2 %>, for a total of <%= die1 + die2 %>.
</p>

rand(1..6) is a whole number from 1 to 6, picked at random. The variables matter: if you wrote rand(1..6) straight into the sentence, the total would come from two new rolls, not the two on the page. Roll once, then use the variables everywhere.

<% %> runs the Ruby and shows nothing; <%= %> runs it and puts the result on the page.

Refresh a few times. Every refresh is a new request, so the view runs again from the top and rolls again.

4. The pictures

The images are named for their number, 1.svg to 6.svg. So the number goes inside the file name:

<p>
  <img src="/images/dice/<%= die1 %>.svg" width="100">
  <img src="/images/dice/<%= die2 %>.svg" width="100">
</p>

When die1 is 4, Rails writes <img src="/images/dice/4.svg">, and the browser asks for that picture. The path starts with /images, not /public/images: everything in public/ is served from the top of the site.

5. Roll again

A link back to the same page asks for it again, which rolls again:

<a href="/dice">Roll again</a>

The whole view

<% die1 = rand(1..6) %>
<% die2 = rand(1..6) %>
<h1>Dice</h1>
<p>
  <img src="/images/dice/<%= die1 %>.svg" width="100">
  <img src="/images/dice/<%= die2 %>.svg" width="100">
</p>
<p>
  You rolled <%= die1 %> and <%= die2 %>, for a total of <%= die1 + die2 %>.
</p>
<a href="/dice">Roll again</a>

Check it against your sentences

Three that the room came up with, and how to check each one by looking at the page:

  • Each die shows a number from 1 to 6. Refresh ten times. You should never see a 0 or a 7.
  • The total is the sum of the two dice. Add them in your head, every refresh.
  • A link rolls again. Click it; the dice change (most of the time: two rolls can match).

If one of yours was "it looks good" or "it's random", try rewording it into something you could count or compare. "It's random" is hard to check by looking once; "the dice aren't the same every time I refresh" is easy.

When it went wrong

  • The same roll every time. A number typed where rand(1..6) belongs.
  • A broken image. The path: /images/dice/3.svg, with the slash at the front and no public/.
  • The total doesn't match the dice. rand(1..6) written into the sentence instead of the variables, so the total rolled its own dice.
  • The generator did nothing. It was typed into the terminal running the server, which isn't taking commands. Use the other one.

For a slower walk through the same page, with the tacos page before it, see
Two Great Things.