人気ブログランキング | 話題のタグを見る

RoR scaffold app/controllers

app ディレクトリの中身はこれ。
~/rails/demo$ ls app
controllers helpers models views

Rails の場合、リンクをクリックしたときに呼び出されるのはコントローラーなので、まず、app/controllers を見てみることにした。
~/ralis/demo$ > ls app/controllers/
application.rb users_controller.rb

ラッキー、ファイルが二つしかない。

ちょっと怖いが application.rb を覗いてみよう。
tomokiyo@tomokiyo-laptop:~/console/rails/demo$ cat app/controllers/application.rb
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time

# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => '53772f42f92fe159d093f65721822d82'

# See ActionController::Base for details
# Uncomment this to filter the contents of submitted sensitive data parameters
# from your application log (in this case, all fields with names like "password").
# filter_parameter_logging :password
end

えっ? たったこれだけ? コメントを除いたら、
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery
end

だけ?

そういえば、下の例のように、ApplicationControllerで、HTMLのヘッダーで文字コードを指定するメソッド set_charset を定義して before_filter に引数として渡してやると、出力する全てのHTMLヘッダに charset=UTF-8 が書き加えられるのだった。

class ApplicationController < ActionController::Base
before_filter :set_charset

private

def set_charset
headers["Content-Type"] = "text/html; charset=UTF-8"
end
end

また、アプリケーション全体の(データベースも含めて)文字コードをUTFにするのには、環境設定なので、config/environment.rb の先頭に

$KCODE = "UTF8"

と、書き込むといいそうだ。出典はここ

こんどは、user モデルのコントローラーを覗いてみた。

~/rails/demo$ cat app/controllers/users_controller.rb
class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
@users = User.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end

# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/new
# GET /users/new.xml
def new
@user = User.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/1/edit
def edit
@user = User.find(params[:id])
end

# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to(@user) }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])

respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
format.html { redirect_to(@user) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy

respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
end
今度はちょっと長い。でも、def で定義されているメソッドはコントローラのアクションに相当することが分かる。users モデルに対して、index, show, new, edit, create, update, destroy などの操作ができるのが分かる。

試しに、FireFoxで http://localhost:3000/users/new にアクセスしたら、新規登録の画面が現れた。やっぱりそうだったんだ。

また、それぞれのメソッドの中身も、respond_to というメソッドにブロックを与えているだけだ。そのブロックの中身も、format.html と format.xml メソッドにブロックを与えている。ブロックを引数とするメソッドの目白押しだ。Ruby というより、関数プログラミングのようだ。

とはいえ、関数プログラミングのように、一つ一つのメソッドの定義の長さが短いのは、コードを読んでいくほうにはたいへんありがたい。

ブロックを引数としているということは、メソッドのどこかで yield しているわけだし、これは、eval code_string しているのとそう変わらない。Ruby on Rails が短いコードで大変なプログラムを作ってしまうことができるのは、こんなメタプログラムが多用されているからではないのだろうか。

しかし、命令文の文字列を実行してしまうメタプログラムはセキュリティ的にはちょっと気持ち悪い気がするが、多分大丈夫なのだろう。どちらにしても、Ruby on Rails はいろいろな意味で楽しめるコーディングのような気がする。作った方も楽しんで作ったのではないだろうか。

最後にひとつ疑問がわいてきた。contoroller のメソッドが実行されたとき、どこで view が呼び出されるのだろうか。コントローラのコードの中には、view の rhtml ファイルを呼び出す箇所はどこにもない。

多分、view を呼び出す機能は、respond_to メソッドが行っているのだろう。また、new アクションが実行される時は、CoC(convention over configuration) のおかげで view のファイル名 app/views/users/new.html.erb を明示的に指定しなくてもよいのだろう。命名の方法に規約を持ち込むことでキー入力を劇的に減らすことが出来て、結果的にバグも劇的に減るにちがいない。
by tnomura9 | 2008-10-13 17:55 | Ruby | Comments(0)
<< RoR scaffold ap... RoR scaffold db >>