トップ «前の日記(2006/06/09 (金) ) 最新 次の日記(2006/06/11 (日) )» 編集 RSS feed

HsbtDiary


2006/06/10 (土) [長年日記]

[Life] ピザパーティー

今日は高専時代の同期のkyounを呼んで、ピザ食べながらサッカー観戦とか。

あと、飲み始める前に研究の被験者ってことで、脳を鍛えるDSトレーニングに出てきそうなアプリケーションの実行を軽く1時間ほどやった。人の学習に関する研究らしく、結果としてもなかなかおもしろげな結果が出たみたいで良かった。

[Rails][ruby]バカが征く on Railsをいじる

railsはモテでrubyは非モテらしいので、暇つぶしにBakagaikuOnRailsをrails-1.1.2(debian/testing)対応っぽく改造作業をしてみた。

migrationの作成

まず、SQLでがりがりテーブルを作るんじゃなくてmigrationで作る。 最初にdebianでrailsを使うときはRails で MySQL Socket の設定 - HsbtDiary (2006-05-06)を参考に設定する。その後に以下のコマンドを実行。

ruby script/generate migration entries
ruby script/generate migration receptions
ruby script/generate migration comments

ファイルの内容はこんな感じ。

001_entries.rb
class Entries < ActiveRecord::Migration
  def self.up
     create_table :entries do |t|
        t.column :id, :primary_key
        t.column :bakaid, :string
        t.column :body, :text
        t.column :reception_id, :int
     end
  end

  def self.down
     drop_table :entries
  end
end
002_receptions.rb
class Receptions < ActiveRecord::Migration
  def self.up
     create_table :receptions do |t|
        t.column :id, :primary_key
        t.column :create_time, :timestamp
     end
  end

  def self.down
     drop_table :receptions
  end
end
003_comments.rb
class Comments < ActiveRecord::Migration
  def self.up
     create_table :comments do |t|
        t.column :id, :primary_key
        t.column :bakaid, :string
        t.column :name, :string
        t.column :body, :text
        t.column :create_time, :timestamp
     end
  end

  def self.down
     drop_table :comments
  end
end

抽象表現とかはてけとう。ファイル内容を変更したら

rake migrate

でテーブルを作成。

models/reception.rbの変更

migrateでの抽象表現timestampはdefault nullが設定されているので、そのままだとreception.create_timeを参照する箇所でnil errorが発生する。そこで、reception.rbでcreateする時に初期値としてTime.nowを設定する。具体的には23行目を以下のように変更。

-reception = create
+reception = create(:create_time => Time.now)

あとはアドレスが変わっているので、

BakaAddress = 'www.jitu.org'

に変更。

views/layouts/application.rbの名前変更

なんかviews/layouts/application.rbだとエラーが発生するので、views/layouts/entry.rbに変更

controllers/application.rbの変更

viewのrhtmlでcharsetを一括して変更する場合はcontrollers/application.rbをいじればいいらしいので該当ファイルを以下のように変更。

class ApplicationController < ActionController::Base
   before_filter :set_charset

   private

   def set_charset
      headers["Content-Type"] = "text/html; charset=EUC-JP"
   end
end

これでとりあえず動いてるっぽい。railsの勉強ついでにRSS生成とか検索も実装してみる?