いや、タイトルは、従業員管理システムというにはあまりにも貧相な結果になりそうなんでやめました。
さて、前回:http://blogs.wankuma.com/kazuki/archive/2008/05/04/136273.aspx
引き続きscaffoldの吐き出すコードを見てみよう。
indexは見たので、お次はshow!
# GET /employees/1
# GET /employees/1.xml
def show
@employee = Employee.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @employee }
end
end
これはindexと殆ど変わらない。
ただ、urlに数字を渡すことで、そのidを持った従業員を探し出すことになってる。
んで、探し出したら後はviewに宜しく~~ってなってる。
好感持てるね。
お次はnewだ!!
# GET /employees/new
# GET /employees/new.xml
def new
@employee = Employee.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @employee }
end
end
これも非常にシンプルだ。
空の従業員を作って、viewによろしく!って丸投げ。これも問題ないね。
んじゃぱぱっと次!edit!
# GET /employees/1/edit
def edit
@employee = Employee.find(params[:id])
end
これは、編集画面表示するだけなのでxml無しみたい。
引数のidで従業員を検索してるだけ。respond_toとか何もしてないので、employees/edit.html.rbが表示される。
んじゃ次!createです。こいつは、今までと違う感じで、なんとifがある!!
# POST /employees
# POST /employees.xml
def create
@employee = Employee.new(params[:employee])
respond_to do |format|
if @employee.save
flash[:notice] = 'Employee was successfully created.'
format.html { redirect_to(@employee) }
format.xml { render :xml => @employee, :status => :created, :location => @employee }
else
format.html { render :action => "new" }
format.xml { render :xml => @employee.errors, :status => :unprocessable_entity }
end
end
end
パラメータから:employeesをとってきて、saveする。
保存に成功したら、redirect_to(@employee)を呼んでる。
なんじゃこれ!ということで、調べに入る。
redirect_toだから、リダイレクトするのはわかるけど…。ぐぐるのもメンドイので動かしてみる。
createアクションなので、従業員作成をしてみよう。
必要な情報を入力してCreateボタンをぽちっとな。
なんだろう…showメソッドあたりにリダイレクトしてるみたいだ。
ちょっと調べてみよう。
…
ん~手持ちのRails本では見つからない記述が結構ある。
どうも手持ちのはRails1.1ベースの本みたいだ。
Web上の別の人の記事とかを見ると、1.1 → 1.2で変わった。1.2 → 2.0でも変わった。って書いてある。
なんてこった。2世代も違う!!
失意の中ちょっと休憩。