続Railsでデモを作れ!

さて、昨日の続き。(でも、商談は落札出来なかったので、あまり意味が無くなった気もするが・・・・)

Rails2.0でのscaffoldの方法が判らないし、Rails1.Xに戻す方法も判らない。
と言うわけで、ググッてみた。
Rails1.xにする方法は
http://blog.japan.zdnet.com/yoshimi/a/2008/01/rails20_1.html
scaffoldの手順は、
http://d.hatena.ne.jp/idesaku/20071211/1197386955

で、折角2.0の操作方法が判ったので、もう少し2.0で進めてみる。
まずは、昨日作ったmigrateの情報を削除して、

$ rm db/migrate/001_create_reports.rb
$ ruby script/generate scaffold report no:string first_report_date:date ditail_report_date:date location:string accident_type:string accident_couse:string depertment:string section:string manager:string comment:string
exists app/models/
exists app/controllers/
exists app/helpers/
exists app/views/reports
exists app/views/layouts/
exists test/functional/
exists test/unit/
overwrite app/views/reports/index.html.erb? (enter "h" for help) [Ynaqdh] y
force app/views/reports/index.html.erb
overwrite app/views/reports/show.html.erb? (enter "h" for help) [Ynaqdh] y
force app/views/reports/show.html.erb
overwrite app/views/reports/new.html.erb? (enter "h" for help) [Ynaqdh] y
force app/views/reports/new.html.erb
overwrite app/views/reports/edit.html.erb? (enter "h" for help) [Ynaqdh] y
force app/views/reports/edit.html.erb
identical app/views/layouts/reports.html.erb
identical public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/report.rb
identical test/unit/report_test.rb
skip test/fixtures/reports.yml
exists db/migrate
create db/migrate/001_create_reports.rb
create app/controllers/reports_controller.rb
create test/functional/reports_controller_test.rb
create app/helpers/reports_helper.rb
route map.resources :reports
$

で、めでたしめでたし。
$ rake db:migrate
を実行しても、何も作成されないしエラーメッセージも出ない。
またしてもmysqlか?と思い

# gem install mysql
Updating metadata for 29 gems from http://gems.rubyforge.org
.............................
complete
Building native extensions. This could take a while...
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

/usr/local/bin/ruby extconf.rb install mysql
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no

extconf.rb failed ***

Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

Provided configuration options:
--with-opt-dir
:
--without-mysqlclientlib


Gem files will remain installed in /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out

となる。
昨日と同じ。/usr/local/lib/ruby/1.8/i686-linuxのしたにmysql.soがあるかと思ったが何もないので
/usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/extconf.rbを編集して、

# make;make install
$ rake db:migrate
(in /home/kkijiya/RailsTest/accident)
/usr/local/bin/ruby: symbol lookup error: /usr/local/lib/ruby/site_ruby/1.8/i686-linux/mysql.so: undefined symbol: mysql_init

・・・・・・またかい。(-_-#
どこかに根本的な間違いがあるらしい。でもmysql.soを作った事で状況が変わったのは事実。
要するにRails2.0用のmysql.soがあればよいのでは?
あるいは、Rails2.0からデフォルトのDBがsqlite3になったことだし、sqlite3でトライしてみるか。

現状では、後者かな。
と言うわけで、プロジェクトの作成からやり直し

$ rails accident02
$ cd accident02/
$ ruby script/generate scaffold report no:string first_report_date:date ditail_report_date:date location:string accident_type:string accident_couse:string depertment:string section:string manager:string comment:string
$ rake db:migrate
(in /home/kkijiya/RailsTest/accident02)
rake aborted!
no such file to load -- sqlite3

(See full trace by running task with --trace)

と、いうわけで、sqlite3-rubyが足りない。

# gem install sqlite3-ruby
Building native extensions. This could take a while...
ERROR: Error installing sqlite3-ruby:
ERROR: Failed to build gem native extension.

/usr/local/bin/ruby extconf.rb install sqlite3-ruby
checking for sqlite3.h... no

make
make: *** `sqlite3_api_wrap.o' に必要なターゲット `ruby.h' を make するルールがありません。中止。


Gem files will remain installed in /usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1 for inspection.
Results logged to /usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1/ext/sqlite3_api/gem_make.out

ってことで、ruby.hがない-> yum install ruby-devel??
違ぁーーう!! rubyはtarでインストールしたじゃん。
だからruby.hは展開した先にあるはずじゃん!
# cp /root/ruby-1.8.6-p111/*.h /usr/local/include/
あと、よく見るとsqlite-develがないよ。

# yum install sqlite-devel
# gem install sqlite3-ruby
Updating metadata for 11 gems from http://gems.rubyforge.org
...........
complete
Building native extensions. This could take a while...
Successfully installed sqlite3-ruby-1.2.1
1 gem installed
Installing ri documentation for sqlite3-ruby-1.2.1...
Installing RDoc documentation for sqlite3-ruby-1.2.1...

よっしゃぁ。入った。
改めて、

$ rake db:migrate
(in /home/kkijiya/RailsTest/accident02)
== 1 CreateReports: migrating =================================================

    • create_table(:reports)

-> 0.0166s
== 1 CreateReports: migrated (0.0191s) ========================================

ふーうう。でけた。

WEBrickをきどうして、http://localhost:3000/reports
で、ちゃんと表示されたぞぉ
これで、画面の基本は出来た。

後、必要なのは、
1.複数のマスタテーブル
2.マスタからコンボボックスを作ること
3.登録時にファイルのアップロード
4.検索条件を指定して一覧

と言うことで、managerマスタとlocationマスタを作ってみる。

$ ruby script/generate scaffold manager section:string manager:string
$ ruby script/generate scaffold location location:string
$ rake db:migrate

ここで、ちょっとSQLiteの使い方を確認
http://lab.lolipop.jp/wordpress/?p=35
を参考に・・・。
$ sqlite db/development.sqlite3
とすると、sqliteなんて知らねぇーーって言われる。
$ sqlite3 db/development.sqlite3 が正解

SQLite version 3.3.6
Enter ".help" for instructions

sqlite> .databases
seq name file

      • --------------- ----------------------------------------------------------

0 main /home/kkijiya/RailsTest/accident03/db/development.sqlite3
1 temp /var/tmp/sqlite_z8wmLc3U7KeOecW
sqlite> .dump
BEGIN TRANSACTION;
CREATE TABLE schema_info (version integer);
INSERT INTO "schema_info" VALUES(3);
CREATE TABLE reports ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "no" varchar(255) DEFAULT NULL, "first_report_date" date DEFAULT NULL, "ditail_report_date" date DEFAULT NULL, "location" integer DEFAULT NULL, "accident_type" varchar(255) DEFAULT NULL, "accident_couse" varchar(255) DEFAULT NULL, "depertment" varchar(255) DEFAULT NULL, "section" integer DEFAULT NULL, "manager" integer DEFAULT NULL, "comment" varchar(255) DEFAULT NULL, "created_at" datetime DEFAULT NULL, "updated_at" datetime DEFAULT NULL);
DELETE FROM sqlite_sequence;
CREATE TABLE locations ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "location" varchar(255) DEFAULT NULL, "created_at" datetime DEFAULT NULL, "updated_at" datetime DEFAULT NULL);
CREATE TABLE managers ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "section" varchar(255) DEFAULT NULL, "manager" varchar(255) DEFAULT NULL, "created_at" datetime DEFAULT NULL, "updated_at" datetime DEFAULT NULL);
COMMIT;

sqlite> select * from reports;
1|001|2008-02-03|2008-02-05|1|1|なんだろうね|どこか|1|1|こんなんでいい?|2008-02-05 14:10:22|2008-02-05 14:10:22
2|002|2008-02-05|2008-02-05|||||||中身なし|2008-02-05 14:10:35|2008-02-05 14:10:35

sqlite> .quit

ってことで.dumpでテーブル定義の中身が見られる。よかったidは自動で作成されてます。

では、外部参照の仕方は?
ってか、このままRails2で進めようか、戻ろうか。
うーーん、また明日m(_ _)m