2012年5月9日水曜日

make Sample Rails Application

※this text from livedoor blog 2009/10/30

Railsのサンプルアプリを作る

MySQLを使うのでアダプタをインストール
$ sudo gem install mysql

アプリケーションのスケルトンを作成
$ cd /var/www
$ sudo rails -d mysql sample
$ cd sample/


productionの項目を設定
$ sudo emacs config/database.yml
production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: sample_production
  pool: 5
  username: root
  password:
  socket: /var/lib/mysql/mysql.sock


rakeコマンドでデータベースを構築
$ sudo rake db:create:all
(in /var/www/sample)


データベースが構築されている事を確認
$ /usr/bin/mysql -uroot -p******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.40-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| sample_development |
| sample_production  |
| sample_test        |
| test               |
+--------------------+
6 rows in set (0.03 sec)

mysql> use sample_production;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> Bye


generateスクリプトでscaffoldを指定して実行
$ sudo ruby script/generate scaffold Sample title:string body:text

マイグレーションファイルの存在を確認
$ cat db/migrate/20091029200846_create_samples.rb
class CreateSamples < ActiveRecord::Migration
  def self.up
    create_table :samples do |t|
      t.string :title
      t.text :body

      t.timestamps
    end
  end

  def self.down
    drop_table :samples
  end
end


rakeコマンドでテーブルを生成
$ sudo rake db:migrate RAILS_ENV=production
(in /var/www/sample)
==  CreateSamples: migrating ==================================================
-- create_table(:samples)
   -> 0.0146s
==  CreateSamples: migrated (0.0151s) =========================================


テーブルが構築されている事を確認
$ /usr/bin/mysql -uroot -p******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.1.40-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use sample_production;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------------------+
| Tables_in_sample_production |
+-----------------------------+
| samples                     |
| schema_migrations           |
+-----------------------------+
2 rows in set (0.00 sec)

mysql> desc samples;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| title      | varchar(255) | YES  |     | NULL    |                |
| body       | text         | YES  |     | NULL    |                |
| created_at | datetime     | YES  |     | NULL    |                |
| updated_at | datetime     | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> Bye


Apacheのconfigファイルを編集
$ sudo emacs /etc/httpd/conf.d/passenger.conf
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
PassengerRuby /usr/bin/ruby

<VirtualHost *:80>
     ServerName www.yourhost.com
     DocumentRoot /var/www/sample/public
</VirtualHost>


httpdのリスタート
$ sudo /etc/init.d/httpd restart

サンプルアプリの画面がブラウザで見れるかチェック
http://www.yourhost.com/samples/




development環境を構築する

developmentの項目を設定
$ sudo emacs config/database.yml
development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: sample_development
  pool: 5
  username: root
  password:
  socket: /var/lib/mysql/mysql.sock


rakeコマンドでテーブルを生成
$ sudo rake db:migrate
(in /var/www/sample)
==  CreateSamples: migrating ==================================================
-- create_table(:samples)
   -> 0.0055s
==  CreateSamples: migrated (0.0061s) =========================================


Apacheのconfigファイルを編集、RailsEnv developmentを追加
$ sudo emacs /etc/httpd/conf.d/passenger.conf
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
PassengerRuby /usr/bin/ruby

<VirtualHost *:80>
     ServerName vmware
     DocumentRoot /var/www/sample/public
     RailsEnv development
</VirtualHost>


httpdのリスタート
$ sudo /etc/init.d/httpd restart

ウェルカム画面をチェック
http://www.yourhost.com/

About your application’s environmentをクリック
Environmentがdevelopmentになっている事を確認
Ruby version    1.8.7 (i686-linux)
RubyGems version    1.3.5
Rack version    1.0
Rails version    2.3.4
Active Record version    2.3.4
Active Resource version    2.3.4
Action Mailer version    2.3.4
Active Support version    2.3.4
Application root    /var/www/sample
Environment    development
Database adapter    mysql
Database schema version    20091029213630


サンプルアプリの画面がブラウザで見れるかチェック
http://www.yourhost.com/samples/