前回:http://blogs.wankuma.com/kazuki/archive/2008/05/04/136242.aspx
前回は、初回で自分でシコシコ書いたコードを捨ててscaffoldで生成されたコードにした。
今回は、ちょっぴりそれを眺めてみようと思う。
眺める対象のコードは、たぶん下の絵にあるコードだと思う。
テストについてもとっかかりたいけど、とりあえず暫くはお預け。
とりあえず、どう動くのが正しいのかを把握しないと、こう動くのが正しい!というのを書くテストもうまいこと回らないだろうから。
さて、modelは第一回目で見たからcontrollerから見ていこう。
ちょっと長いけど、employees_controller.rbの中身を張ってみる。
class EmployeesController < ApplicationController
# GET /employees
# GET /employees.xml
def index
@employees = Employee.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @employees }
end
end
# 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
# 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
# GET /employees/1/edit
def edit
@employee = Employee.find(params[:id])
end
# 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
# PUT /employees/1
# PUT /employees/1.xml
def update
@employee = Employee.find(params[:id])
respond_to do |format|
if @employee.update_attributes(params[:employee])
flash[:notice] = 'Employee was successfully updated.'
format.html { redirect_to(@employee) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @employee.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.xml
def destroy
@employee = Employee.find(params[:id])
@employee.destroy
respond_to do |format|
format.html { redirect_to(employees_url) }
format.xml { head :ok }
end
end
end
scaffoldによって、それなりの量のコードが吐かれてる。
とりあえず、デフォルトで呼ばれるindexアクションから見てみようと思う。
上のコードからindexアクションの部分だけ抜粋!
# GET /employees
# GET /employees.xml
def index
@employees = Employee.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @employees }
end
end
コメントを見る限り、無印とxmlをサポートしているみたいだ。
無印はhtmlっぽい。
手持ちのRails本にはrespond_toが見当たらない。Rails2.0からの機能なのかな?
軽く調べたら、同じデータをもとに色んな形の出力をしやすくなってるみたいだ。
format.htmlってやると、index.html.erbが呼ばれる。
format.拡張子 { ブロック }の形にすると、その拡張子が来た時に、ブロック内の処理が呼ばれるんじゃないかと感覚的に思う。
試しにtext形式もサポートするように変えてみようかな…
っとその前に!!この一覧を表示するアクションでは、無条件に従業員を全部取得して@employeesに入れてViewに宜しく!ってしてる。
うむ。正しいMVCのコードになってるね!!好感持てるよ。
じゃぁどれくらい簡単にViewにテキスト形式を追加できるかやってみようと思う。
感覚的にはindexのrespond_toの所に下のようなコードを足すことになる。
format.txt { render :inline => "Hello world" }
さくっと実行!http://localhost:3000/employees.txtにアクセスしてみた。
エラーorz
NameError in EmployeesController#index
uninitialized constant Mime::TXT
respond_toのヘルプを読むとmime登録しなよ!的な事が英語で書いてあった。TOEIC300以下の読解力の持ち主が読解したからあってるかどうかは不明。
とりあえず、environment.rbの末尾に定義を追加してみた。
Mime::Type.register_alias("text/plane", :txt)
一度WEBrickを落として再実行!
おっ出た出た。
そして、format.txtの後にブロック渡さなかったらindex.txt.erbの結果が出てくるみたいだ。
早速やってみた。
コントローラのformat.txtの後のブロックを消す。
# GET /employees
# GET /employees.xml
def index
@employees = Employee.find(:all)
respond_to do |format|
format.html # index.html.erb
format.txt # index.txt.erbを使っておくれ
format.xml { render :xml => @employees }
end
end
んで、Viewsの下のemployeesにindex.txt.erbという名前でファイルを作った。中身は、従業員の名前だけを出す形にしてみた。
<%- @employees.each do |employee| -%>
<%= employee.name %>
<%- end -%>
実行!!
改行がブラウザでちゃんと表示されてないのはなぜだろう。
mime-typeあたりがちゃんと設定されてないのかな??
まぁ、後で実際に使うことになったらもうちょっと掘り下げるとして動作確認できたのでOK!
次からは、ほかのアクションも見ていこう。
投稿日時 : 2008年5月4日 15:23