« 環境変数を簡単に変更する | メイン | [Firefox]Screen grab! »
2007年07月10日
JRubyを使ってみる 其の3
さて、前回の続きでDBとの連係をしていきます。
まずはDBはMySQLを用意。インストールは適当にぐぐってやる。
テーブルの作成とはMySQL Query Browserがあると便利。無料。適当にぐぐってインストール。
Query BrowserでDBとテーブルを作る。
DB名はとりあえずrailssample。テーブル名はtodos。
Sampleなので愛着が湧かないように超適当。
Query BrowserではGUIで作成したテーブルのSQLを自動生成してくれる。以下のような感じのSQLになる。
DROP TABLE IF EXISTS `railssample`.`todos`;
CREATE TABLE `railssample`.`todos` (
`id` int(10) unsigned NOT NULL auto_increment,
`task` varchar(45) NOT NULL,
`details` varchar(45) default NULL,
`status` varchar(45) NOT NULL,
`limit` date default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
次、railsコマンドで作成したアプリ(今回で言えばfoo)の
configディレクトリの下にある、database.ymlを編集する。
development(開発)とtest(試験)とproduction(本番)でDBを分けることを半ば強制される。
人間は強制されないとめんどっちいことはしない。ので、とてもいいことだと思う。
でも、今回はsampleなので駄目だとわかっていながら同じDBを使う。
編集する内容
adapter: データベースの種類("mysql"とか)
database: データベース名
username: ユーザ名
password: パスワード
host: ホスト名、またはIP
お次にscaffold(足場)を作る。
C:\JRuby\sample_apps\foo>jruby script\generate scaffold todos todos
exists app/controllers/
exists app/helpers/
exists app/views/todos
exists app/views/layouts/
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/todos.rb
identical test/unit/todos_test.rb
identical test/fixtures/todos.yml
create app/views/todos/_form.rhtml
create app/views/todos/list.rhtml
create app/views/todos/show.rhtml
create app/views/todos/new.rhtml
create app/views/todos/edit.rhtml
create app/controllers/todos_controller.rb
create test/functional/todos_controller_test.rb
create app/helpers/todos_helper.rb
create app/views/layouts/todos.rhtml
identical public/stylesheets/scaffold.css
JRubyでも問題なく生成することができる。
ではでは、動作確認。まずはWEBrickの起動。
C:\JRuby\sample_apps\foo>jruby script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-07-10 00:03:56] INFO WEBrick 1.3.1
[2007-07-10 00:03:56] INFO ruby 1.8.5 (2007-06-07) [java]
[2007-07-10 00:03:56] INFO WEBrick::HTTPServer#start: pid=14737862 port=3000
ブラウザから確認。
http://127.0.0.1:3000/todos/
無問題。
今回はここまで。
次回はJRubyでajax_scaffoldがうまく動くか試す。
ラトルズ (2007/03)
売り上げランキング: 39688
趣味感覚でRubyを始めるにはぴったりの入門書
投稿者 2ca3 : 2007年07月10日 01:05
トラックバック
このエントリーのトラックバックURL:
http://2ca3.dyndns.org/cgi-bin/mt/mt-tb.cgi/151
