Rails作業記録その2

MySQLの設定

作業を進める前にDB接続のためのパスワードを設定しておく。

/home/www/2555$ e config/database.yml

development:
  adapter: mysql
  encoding: utf8
  database: 2555_development
  username: root
  password: pass
  socket: /var/run/mysqld/mysqld.sock
         :
         :
         :

ActiveScaffoldのインストール

流行なので入れておく。普通に入れると「Ver1.0.1」がインストールされてしまって、Rails2.0に対応していないそうなので、「1.1.0 RC1」を入れる。(← 4/25現在)

/home/www/2555$ ruby script/plugin install http://activescaffold.googlecode.com/svn/tags/1.1.0_rc1
 + ./CHANGELOG
 + ./MIT-LICENSE
 + ./README
   :
   :
 + ./test/run_all.rb
 + ./test/test_helper.rb
 + ./uninstall.rb
/home/www/2555$ mv vendor/plugins/1.1.0_rc1 vendor/plugins/active_scaffold

「vendor/plugins/1.1.0_rc1」を「vendor/plugins/active_scaffold」にリネームしておくことを忘れずに。
ついでにActiveScaffoldの日本語化プラグインも入れておく。

/home/www/2555$ ruby script/plugin install http://svn2.assembla.com/svn/activescaffold_japanese_l10n
 + ./README
 + ./Rakefile
 + ./init.rb
 + ./install.rb
 + ./lib/activescaffold_japanese_l10n.rb
 + ./lib/locale_ja_jp.rb
 + ./tasks/activescaffold_japanese_l10n_tasks.rake
 + ./test/activescaffold_japanese_l10n_test.rb
 + ./uninstall.rb

Modelの作成

/home/www/2555$ ruby script/generate model item
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/item.rb
      create  test/unit/item_test.rb
      create  test/fixtures/items.yml
      exists  db/migrate
      create  db/migrate/001_create_items.rb

テーブルの作成

「db/migrate/001_create_items.rb」を編集する。(毎回毎回「sudo nano」と打つのは面倒臭いので「alias e='sudo nano'」にしてある)

/home/www/2555$ e db/migrate/001_create_items.rb

class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.primary_key :id
      t.string      :itemName,      :null => false, :default => ''      # 商品名
      t.string      :itemCode,      :null => false, :default => ''      # 商品コード
      t.decimal     :itemPrice,     :null => false, :default => 0.0, :precision => 8, :scale => 2
          :
          :
      t.timestamps
    end
  end

「t.timestamps」とすると、「created_at」、「updated_at」カラムが自動で生成される。
さて「db:migrate」を実行してみる。

/home/www/2555$ rake db:migrate
 (in /home/www/2555)
 == 1 CreateItems: migrating ===================================================
 -- create_table(:items)
    -> 0.0441s
 == 1 CreateItems: migrated (0.0448s) ==========================================

無事完了した模様。

Controllerの作成

/home/www/2555$ ruby script/generate controller item
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/item
      exists  test/functional/
      create  app/controllers/item_controller.rb
      create  test/functional/item_controller_test.rb
      create  app/helpers/item_helper.rb

「app/controllers/item_controller.rb」を編集します。

/home/www/2555$ e app/controllers/item_controller.rb

class ItemController < ApplicationController
  active_scaffold
  layout "item"
end

レイアウトの作成

/home/www/2555$ e app/views/layouts/item.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title>Items: <%= controller.action_name %></title>
    <%= javascript_include_tag :defaults %>
    <%= active_scaffold_includes %>
  </head>
  <body>
    <p style="color: green"><%= flash[:notice] %></p>
    <%= yield  %>
  </body>
</html>

サーバ起動

/home/www/2555$ ruby script/server

=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2008-04-26 14:07:15] INFO  WEBrick 1.3.1
[2008-04-26 14:07:15] INFO  ruby 1.8.6 (2007-06-07) [i486-linux]
[2008-04-26 14:07:15] INFO  WEBrick::HTTPServer#start: pid=17330 port=3000

http://192.168.1.xxx:3000/item」でこの画面が出たのでOK。

参考

  1. 2007-12-20」は相変わらずお奨め。
  2. Ruby on Rails - ActiveRecord - — ありえるえりあ」。よくまとまっています。
  3. booleanとtinyintはどうしようか考え中。参考→ARはMySQLでtinyint(1)をbooleanにエミュレートする - Lazy TechnologyMySQLのMigrationで:tinyintを有効にする方法 - Lazy Technology