読者です 読者をやめる 読者になる 読者になる

※yumコマンドだと最新版のrubyが手に入らないので、rubyパッケージ削除してからソースからビルドする

#yumでインストールされたrubyパッケージの削除

yum remove ruby

#rubyのビルド

http://www.ruby-lang.org/ja/downloads/からrubyをダウンロードしてから
tar -zxfv ruby-1.8.6.tar.gz
cd ruby-1.8.6
./configure
make
make install



#ruby専用のパッケージ管理で使用するRubyGemsをセットアップ

wget http://rubyforge.org/frs/download.php/5207/rubygems-0.9.4.tgz
tar xfz rubygems-0.9.4.tgz
cd rubygems-0.9.4
su -c "ruby setup.rb"



#gemコマンド(RubyGems)でrailsパッケージをインストールする

su -c "gem install rails --include-dependencies"




#mysqlを起動して今回のサンプルアプリ用のデータベースを作成する

/etc/init.d/mysqld start
mysql -u root

#エンコード設定
SET NAMES utf8;
#データベース作成
CREATE DATABASE addressbook;
#データベース切り替え
USE addressbook;
#テーブル作成
CREATE TABLE people (
id int(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(64),
mail VARCHAR(128),
tel VARCHAR(13)
);
#abユーザーにCRUD権限を設定
GRANT ALL PRIVILEGES ON addressbook.* TO ab@localhost IDENTIFIED BY 'ab';

quit



#railsアプリケーションの自動生成

rails addressbook



#データベース接続設定

vi addressbook/config/database.yml
database: addressbook ←addressbook_developmentから変更
username: ab ←rootから変更

#データベースマッピングオブジェクトの自動生成

ruby script/generate model Person

#上記オブジェクトを操作するオブジェクトの自動生成

ruby script/generate controller address


#設定追加

vi addressbook/app/controllers/address_controller.rb
class AddressController < ApplicationController
scaffold :person ← 追加
end


#付属のhttpサーバー起動

ruby script/server
※デフォルトで3000ポートを使用する(他ポートで起動したい場合は-p 3001をつける)




http://localhost:3000/address/にアクセスすればサンプルアプリが動く(はず)



追記
WEBrickサーバー(Ruby付属のWEBサーバー起動)を使って起動してアクセスするとコンソールに以下のエラーが出る

#
["/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:404:in `to_constant_name'", "/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:214:in `qualified_name_for'", "/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:476:in `const_missing'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:117:in `reset_after_dispatch'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:51:in `dispatch'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:113:in `handle_dispatch'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in `service'", "/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'", "/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'", "/usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'", "/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start'", "/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'", "/usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start'", "/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `each'", "/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'", "/usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'", "/usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in `dispatch'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/webrick.rb:59", "/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'", "/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'", "/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'", "/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'", "/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39", "/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'", "/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'", "./server:3"]
[2007-07-16 23:23:11] ERROR `/contacts/' not found.
192.168.1.3 - - [16/Jul/2007:23:23:11 JST] "GET /contacts/ HTTP/1.1" 404 281

  • -> /contacts/


log/development.logには以下のように出る。

no such file to load -- ../config/../config/routes.rb
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1165:in `load_routes!'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1157:in `reload'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:105:in `prepare_application'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:39:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:113:in `handle_dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in `service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/webrick.rb:59
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
./server:3

調べたらWEBrickサーバー起動するときにscriptディレクトリから
./server
で起動するとだめらしい。

アプリケーションルートから
./script/server
で起動すれば大丈夫でした。

なんてこった。。。