まったり Rails(その4)

さて、いよいよscaffoldを試してみよう。
といっても、通常通りコントローラクラスをRailsのコマンドを使って生成し、その冒頭でscaffoldを使うことを宣言するだけなので、何もすることが無い。

  • 実行したコマンド
ruby script/generate controller admin
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/admin
      exists  test/functional/
      create  app/controllers/admin_controller.rb
      create  test/functional/admin_controller_test.rb
      create  app/helpers/admin_helper.rb

これでモデル"admin"アクションにおけるコントローラのプレースホルダが出来た。あとはコントローラをちょちょいと書き直す。

  • admin_controller.rb
class AdminController < ApplicationController
  scaffold :product
end

では、早速サーバを起動して"足場"を試してみよう。

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
:
:

おっとここでついに予想外のエラー。"undefined method `scaffold' for AdminController:Class"とある。
どうして未定義のメソッドを使ったことになるんだろう。"scaffold"の綴りを5回位見直したが、シンタックスの問題でもない。今更足りないものがあるのか??

5分位悩んだ後(たった5分!)、InfoQで以下の記事を見つけた。

Rails 2.0 - 初心者がすべきことは?
Rails 2.0 - What's a Newbie to Do?(原文)
なるほど、Rails2.0からはデフォルトのデータベースがMySQLからsqliteになっただけではなく、scaffolding等のプラグインがデフォルトではインストールされなくなっているらしい。

このように書籍と実際のソフトウェアの環境や動作が一致しないのはRailsに限らず開発の早いオープンソースではよくあることであり、ここで躓いてその後止めてしまう懸念がある。私同様に初学者は注意する所だろう。

さて、インストールされていないのであれば、とっととインストールしてしまおう。プラグインを追加でインストールするのも簡単だ。

>ruby script/plugin install scaffolding
+ ./MIT-LICENSE
+ ./README
+ ./Rakefile
+ ./init.rb
+ ./lib/scaffolding.rb
+ ./lib/templates/edit.erb
+ ./lib/templates/layout.erb
+ ./lib/templates/list.erb
+ ./lib/templates/new.erb
+ ./lib/templates/show.erb
+ ./test/scaffolding_test.rb

これで良いと思ったのだが、実行すると今度は以下のエラーメッセージだ。

undefined method `paginate' for #

今度は書いた覚えが無いメソッドが未定義と怒られている。AdminControllerのインスタンスでエラーが発生しているってことは、scaffold生成中に発生したのか?

どうやら今度は"classic_pagination"というプラグインが必要のようだ。

なので、今度も早速インストール

ruby script/plugin install http://tools.assembla.com/svn/breakout/breakout/vendor/plugins/classic_pagination
+ ./CHANGELOG
+ ./README
+ ./Rakefile
+ ./init.rb
+ ./install.rb
+ ./lib/pagination.rb
+ ./lib/pagination_helper.rb
+ ./test/fixtures/companies.yml
+ ./test/fixtures/company.rb
+ ./test/fixtures/developer.rb
+ ./test/fixtures/developers.yml
+ ./test/fixtures/developers_projects.yml
+ ./test/fixtures/project.rb
+ ./test/fixtures/projects.yml
+ ./test/fixtures/replies.yml
+ ./test/fixtures/reply.rb
+ ./test/fixtures/schema.sql
+ ./test/fixtures/topic.rb
+ ./test/fixtures/topics.yml
+ ./test/helper.rb
+ ./test/pagination_helper_test.rb
+ ./test/pagination_test.rb


Pagination
==========

To install:

  script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination

This code was extracted from Rails trunk after the release 1.2.3.
WARNING: this code is dead. It is unmaintained, untested and full of cruft.

There is a much better pagination plugin called will_paginate.
Install it like this and glance through the README:

  script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

It doesn't have the same API, but is in fact much nicer. You can
have both plugins installed until you change your controller/view code that
handles pagination. Then, simply uninstall classic_pagination.

何々、will_paginateプラグインをインストールした方が良いと? うむむむ。

今度はちょっと疑念を抱きつつも、will_paginateプラグインのURLを調べてインストール。

ruby script/plugin install http://tools.assembla.com/svn/breakout/breakout/vendor/plugins/will_paginate
+ ./LICENSE
+ ./README
+ ./Rakefile
+ ./init.rb
+ ./lib/will_paginate/collection.rb
+ ./lib/will_paginate/core_ext.rb
+ ./lib/will_paginate/finder.rb
+ ./lib/will_paginate/view_helpers.rb
+ ./lib/will_paginate.rb
+ ./test/array_pagination_test.rb
+ ./test/boot.rb
+ ./test/console
+ ./test/finder_test.rb
+ ./test/fixtures/admin.rb
+ ./test/fixtures/companies.yml
+ ./test/fixtures/company.rb
+ ./test/fixtures/developer.rb
+ ./test/fixtures/developers_projects.yml
+ ./test/fixtures/project.rb
+ ./test/fixtures/projects.yml
+ ./test/fixtures/replies.yml
+ ./test/fixtures/reply.rb
+ ./test/fixtures/schema.rb
+ ./test/fixtures/schema.sql
+ ./test/fixtures/topic.rb
+ ./test/fixtures/topics.yml
+ ./test/fixtures/user.rb
+ ./test/fixtures/users.yml
+ ./test/helper.rb
+ ./test/lib/activerecord_test_case.rb
+ ./test/lib/activerecord_test_connector.rb
+ ./test/lib/load_fixtures.rb
+ ./test/pagination_test.rb

これでやっと"足場"を拝む所までこれた。

噂に聞くより随分と時間がかかってしまったが、いきなり2.0系に初心者が手をだすなということなのだろうか。