Rfmのサンプルサイトを動かす準備

Rfmを使い始める最も簡単な方法は、サンプルサイトのソースコードをダウンロードすることであると前のエントリーで述べました。しかしながら、現在配布されているRfmのサンプルサイトのファイルはRails 1.2.3のときに作成されたものであり、Rails 2.3.2で動かすにはいろいろと修正を加えなければなりません。

圧縮ファイル「rfm_examples.zip」の中には「README」という名前のテキストファイルがあります。READMEには基本的な使い方について記述されていますので、圧縮ファイルを展開した後はまず最初にREADMEに目を通すようにしましょう。

READMEにあるように、cdコマンドを使用して「rails-sample」ディレクトリに移動し、「ruby script/server」とタイプするとサンプルサイトをRailsアプリケーションとして動かすことができます。ただし、現時点では多くの場合、まず最初に次のようなエラーメッセージが表示される可能性があります。

$ ruby script/server
Cannot find gem for Rails ~>1.2.3.0:
Install the missing gem with 'gem install -v=1.2.3 rails', or
change environment.rb to define RAILS_GEM_VERSION with your desired version.

上記のエラーメッセージが出た場合には、config/environment.rbにおいて「RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION」の行にある「1.2.3」を「2.3.2」に変更し、再度「ruby script/server」とタイプすると、次のようなエラーメッセージが表示されるでしょう。

$ ruby script/server
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
Your config/boot.rb is outdated: Run "rake rails:update".

上記のエラーメッセージが出た場合には、エラーメッセージにあるように「rake rails:update」とタイプしてrakeコマンドを実行します(rakeについてはここでは特に解説しません)。

気を取り直して再度「ruby script/server」とタイプすると、今度は次のようなエラーメッセージが表示されるかもしれません。

$ ruby script/server
Rails requires RubyGems >= 1.3.1 (you have 1.0.1). Please `gem update --system` and try again.

エラーメッセージをきちんと読めば、バージョン1.3.1以降のRubyGemsが要求されていることが分かります。「sudo gem update --system」(Windowsなら「gem update --system」)を実行することでRubyGemsを更新できます。ただし、環境によっては「gem install rubygems-update」を実行した後、「update_rubygems」を実行する手順でないと更新できない場合もあります。

$ sudo gem update --system
Password:
Updating RubyGems...
Bulk updating Gem source index for: http://gems.rubyforge.org
Attempting remote update of rubygems-update
Successfully installed rubygems-update-1.3.1
1 gem installed
Updating version of RubyGems to 1.3.1
Installing RubyGems 1.3.1
(以下略)

RubyGemsを更新した後に「ruby script/server」とタイプしても、まだRailsを起動させることはできません。さらにconfig/environments/development.rbを書き換える必要があります。

$ ruby script/server
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000

*******************************************************************
* config.breakpoint_server has been deprecated and has no effect. *
*******************************************************************

/Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:580:in `send': undefined method `cache_template_extensions=' for ActionView::Base:Class (NoMethodError)
from /Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:580:in `initialize_framework_settings'
from /Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:579:in `each'
(以下略)

config/environments/development.rbにおいて、「config.action_view.cache_template_extensions」で始まる行と「config.breakpoint_server」で始まる行の先頭に「#」をつけてコメントアウトすると、エラーメッセージが変わります。

$ ruby script/server
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/session/cookie_store.rb:163:in `ensure_session_key': A key is required to write a cookie containing the session data. Use config.action_controller.session = { :key => "_myapp_session", :secret => "some secret phrase" } in config/environment.rb (ArgumentError)
from /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/session/cookie_store.rb:74:in `initialize'
(以下略)

エラーメッセージを読むと、config/environment.rbに「config.action_controller.session」の設定を追加すればよさそうなことが分かります。config/environment.rbにおいて、「Rails::Initializer.run do |config|」と「end」の間のどこかに「config.action_controller.session = { :key => "_myapp_session", :secret => "some secret phrase" }」を追加すると、さらにエラーメッセージが変わります。

$ ruby script/server
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/session/cookie_store.rb:188:in `ensure_secret_secure': Secret should be something secure, like "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx". The value you provided, "some secret phrase", is shorter than the minimum length of 30 characters (ArgumentError)
from /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/session/cookie_store.rb:78:in `initialize'
(以下略)

エラーメッセージを読めば、さきほど設定した「some secret phrase」という文字列のままでは秘密の値にはならないので、安全性のためにきちんと秘密の値を設定する必要があることが分かります。上記のエラーメッセージ内に含まれる「like "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"」の部分は毎回出力される文字列が変わるようですので、値の設定に迷った場合にはこの値を使ってもいいかもしれません。

エラーが発生する要因がなくなってから「ruby script/server」とタイプして、Webブラウザーでhttp://localhost:3000/にアクセスし、「Welcome to the RFM Test Site!」と表示されれば第1段階は成功です。ただし、残念ながら「Welcome to the RFM Test Site!」とは表示されずに、「NoMethodError in WelcomeController#index」というエラーメッセージが表示されてしまうはずです。まだまだ修正しなければならない箇所があります。

$ ruby script/server
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server