ActiveScaffoldを使ってみた
id:yuum3:20080702:1215004076に書いた同胞メールソフトの顧客データ管理用テーブルのメンテナンスツールを ActiveScaffoldを使って作る事にしました。
コントローラを作成
顧客データは 顧客 coustomer と、タグ tag, その関連の tagging テーブルがありますが、今回は coustomerのメンテがメイン。
script/generate controller Customers
ActiveScaffoldのインストール
検索すると、ActiveScaffoldはRails2.1で動かないとか、動くとか色々でてきますが。現在 gitにあるmaster(1.2RC1)は Rails 2.1 に対応してます。
script/plugin install git://github.com/activescaffold/active_scaffold.git
Subversion 版は 1.1.1 でRails2.1 には対応してません。 もうSubversionの時代は終わってしまったのですね・・・
gitのインストールも手間取ったのですが、それはまたの機会に・・・
ActiveScaffold用のコードを追加
ActiveScaffoldのページに書かれているように、layout, controllerに数行書くだけ。
## app/views/layouts/customers.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>Todos: <%= controller.action_name %></title> <%= javascript_include_tag :defaults %> ## <=== 変更 <%= active_scaffold_includes %> ## <=== 追加 </head> <body> <p style="color: green"><%= flash[:notice] %></p> <%= yield %> </body> </html> # app/controllers/customers_controller.rb class CustomersController < ApplicationController active_scaffold :customer ## <=== 追加 end
これだけで ↓ こんな感じのAjaxな CRUD画面が表示されます。coustomer と関連してる tag の内容も表示されてます。
カスタマイズ
舞波さんのページ (ありがとうございます!) を参考に表示をカスタマイズ
class CustomersController < ApplicationController active_scaffold :customer do |config| config.list.per_page = 10 config.list.sorting = {:id => :asc} config.label = "顧客データ" config.columns = [:id, :name, :company, :email, :tags, :created_at] config.columns[:name].label = "名前" config.columns[:company].label = "会社" config.columns[:created_at].label = "登録日" config.columns[:tags].label = "タグ" config.columns[:name].required = true end end
メニューの日本語化
ActiveScaffold Japanese L10Nプラグイン (ありがとうございます!) をインストール。
script/plugin install http://svn2.assembla.com/svn/activescaffold_japanese_l10n
レコードの編集
現在、日付とtag入力が変です・・・・ たぶん次回へ続く・・・ :-)