おみくじアプリケーションの作成

p.8〜10まで。generateでcontrollerとviewを作ってそれらの連携を確かめる。まだmodelは使わない。


ここまでをbitbucketにpushした。
http://bitbucket.org/shokai/web-innovative/changeset/00311880fc28/

rails _2.0.2_ omikuji
cd omikuji
ruby script/generate controller main index
ruby script/server


app/controllers/main_controller.rb

class MainController < ApplicationController

  def index
    kuji = rand(6) # 0から5の乱数
    case kuji
    when 0
      @message = "大吉"
    when 1
      @message = "中吉"
    when 2
      @message = "小吉"
    when 3
      @message = ""
    when 4
      @message = ""
    when 5
      @message = "大凶"
    end
    @cdate = Date.today
  end
end


app/views/main/index.html.erb

<h1>あなたの今日の運勢は <%= @message %> です</h1>
<%= @cdate %>

これでおみくじができた。
http://localhost:3000/main/index にアクセスすると、日時と運勢(ランダム)が表示される。

index