小さい頃はエラ呼吸

いつのまにやら肺で呼吸をしています。


Ruby On Rails2.2で作るサンプルアプリケーション(ON さくらインターネット)


さくらインターネットでRuby on Rails2.2とMySQLを使ったアプリケーションを作ってみた。つまづいた部分が多々あったので、忘れないようにメモしておく。

Ruby on Railのインストール

以下を参考にRuby on Railのインストールを行う。

アプリケーションの作成

サンプルとしてブックマークを管理するアプリケーションを作ってみる。
1. アプリケーションを格納するディレクトリを作成する

mkdir Rails
cd Rails

2. Bookmarkという名前のMySQL用アプリケーションを作成する

%rails Bookmark -d mysql
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  config/initializers
      create  config/locales
      create  db
      create  doc
      create  lib
      create  lib/tasks
      create  log
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  script/performance
      create  script/process
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance
      create  test/unit
      create  vendor
      create  vendor/plugins
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  Rakefile
      create  README
      create  app/controllers/application.rb
      create  app/helpers/application_helper.rb
      create  test/test_helper.rb
      create  test/performance/browsing_test.rb
      create  config/database.yml
      create  config/routes.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_rails_defaults.rb
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/environment.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/environments/test.rb
      create  script/about
      create  script/console
      create  script/dbconsole
      create  script/destroy
      create  script/generate
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  script/performance/request
      create  script/process/reaper
      create  script/process/spawner
      create  script/process/inspector
      create  script/runner
      create  script/server
      create  script/plugin
      create  public/dispatch.rb
      create  public/dispatch.cgi
      create  public/dispatch.fcgi
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/index.html
      create  public/favicon.ico
      create  public/robots.txt
      create  public/images/rails.png
      create  public/javascripts/prototype.js
      create  public/javascripts/effects.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/controls.js
      create  public/javascripts/application.js
      create  doc/README_FOR_APP
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log

3. MySQLに接続し、以下のようなテーブルを作成する。

カラム名 データ型 NOT NULL その他
ID int(11) NOT NULL 主キー、AutoIncrement
NAME varchar(255) NOT NULL  
URL varchar(255 NOT NULL  
CREATE TABLE `bookmarks` (
  `id` int(11) NOT NULL default '0',
  `name` varchar(255) NOT NULL default '',
  `url` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;
2009.03.23追記

※4および5の処理を先に行い、以下のようにrakeコマンドでテーブルを自動生成することもできる。

%rake db:migrate
(in /home/XXX/Rails/Bookmark)
==  CreateBookmarks: migrating ================================================
-- create_table(:bookmarks)
   -> 0.0172s
==  CreateBookmarks: migrated (0.0174s) =======================================

4. Bookmark/config/database.ymlというデータベースの設定ファイルを以下のように編集する

development:
  adapter: mysql
  encoding: utf8
  database: データベース名
  pool: 5
  username: ユーザ名
  password: パスワード
  host: XXX.db.sakura.ne.jp←MySQLのサーバ名

5. scaffoldで画面やモデル、コントローラを自動生成する。

%script/generate scaffold bookmark name:string url:string
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/bookmarks
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      exists  public/stylesheets/
      create  app/views/bookmarks/index.html.erb
      create  app/views/bookmarks/show.html.erb
      create  app/views/bookmarks/new.html.erb
      create  app/views/bookmarks/edit.html.erb
      create  app/views/layouts/bookmarks.html.erb
      create  public/stylesheets/scaffold.css
      create  app/controllers/bookmarks_controller.rb
      create  test/functional/bookmarks_controller_test.rb
      create  app/helpers/bookmarks_helper.rb
       route  map.resources :bookmarks
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/bookmark.rb
      create    test/unit/bookmark_test.rb
      create    test/fixtures/bookmarks.yml
      create    db/migrate
      create    db/migrate/20090314074435_create_bookmarks.rb

6. 公開用ディレクトリにシンボリックリンクを貼る

ln -s $HOME/Rails/Bookmark/public $HOME/www/Bookmark

7. http://xxx.sakura.ne.jp/Bookmark/にアクセスしてみる。
以下のようなページが表示される。

トラブルシューティングおよび環境設定

About your application’s environmentをクリックしたときに、さまざまなエラーが発生する可能性がある。それらのエラーに対して、1つ1つ対処していく。

Not Found The requested URL /Bookmark/rails/info/properties was not found on this server.

/Bookmark/public/配下に、以下のような.htaccessファイルを配置する。

# General Apache options
#AddHandler fastcgi-script .fcgi
#AddHandler cgi-script .cgi
#Options +FollowSymLinks +ExecCGI

# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests
# 
# Example:
#   RewriteCond %{REQUEST_URI} ^/notrails.*
#   RewriteRule .* - [L]

# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
# 
# For better performance replace the dispatcher with the fastcgi one
#
# Example:
#   RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteEngine On

# If your Rails application is accessed via an Alias directive,
# then you MUST also set the RewriteBase in this htaccess file.
#
# Example:
#   Alias /myrailsapp /path/to/myrailsapp/public
#   RewriteBase /myrailsapp

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

# In case Rails experiences terminal errors
# Instead of displaying this message you can supply a file here which will be rendered instead
# 
# Example:
#   ErrorDocument 500 /500.html

ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
Application error Rails application failed to start properly"

/Bookmark/config/environment.rbを編集し、先頭に以下を追加する。※xxxは、ユーザIDを指定する。

$LOAD_PATH.push("/home/xxx/lib/ruby")
$LOAD_PATH.push("/home/xxx/lib")
ENV['GEM_HOME'] ||= '/home/xxx/lib/ruby/gem'
No route matches "/Bookmark/rails/info/properties" with {:method=>:get}

/Bookmark/config/environment.rbを編集し、先頭に以下を追加する。

ENV['RAILS_RELATIVE_URL_ROOT']="/Bookmark"
Can't initialize character set utf8 (path: /usr/local/share/mysql/charsets/)

/Bookmark/config/database.ymlを編集し、encodingをコメントアウトする。

development:
  adapter: mysql
  #encoding: utf8
  database: データベース名
  pool: 5
  username: ユーザ名
  password: パスワード
  host: XXX.db.sakura.ne.jp←MySQLのサーバ名

設定完了

以下のような画面が表示されれば、設定は完了。

http://xxx.sakura.ne.jp/Bookmark/bookmarksにアクセスすれば、簡単なブックマーク管理アプリが作成されている。