ActiveScaffold
いままで内蔵しているScaffoldを使用してCRUDを構築してきましたが、さらに機能アップされた、また見栄えのよいActiveScaffoldを取り上げます。
1-1. プロジェクトの生成
(1) プロジェクトAppli008の生成
(2) 日本語環境の設定
(3) データベースの作成
1-2. 住所録テーブルAddressの作成
(1) モデルの生成
NetBeansで[生成]を選択
ジェネレート(G):model
引数(A):Address (モデル名の先頭文字は大文字とすること)
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/address.rb
create test/unit/address_test.rb
create test/fixtures/addresses.yml
create db/migrate
create db/migrate/20100204041928_create_addresses.rb
(2) マイグレーションファイルの修正
class CreateAddresses < ActiveRecord::Migration
def self.up
create_table :addresses do |t|
t.string :namae
t.string :yubin, :limit=>8
t.string :jusho
t.string :denwa, :limit=>20
t.timestamps
end
enddef self.down
drop_table :addresses
end
end
(3) マイグレーションの実行
NetBeansで[データベースマイグレーション]より[現在のバージョンへ]を選択します。
(in D:/Rails_Projects/Appli008)
== CreateAddresses: migrating ================================================
-- create_table(:addresses)
-> 0.1090s
== CreateAddresses: migrated (0.1090s) =======================================
1-3. ActiveScaffoldのインストール
(1) github.comからのダウンロード
IE以外のブラウザを使いhttp://activescaffold.com/でメインのホームページを呼び出し
をクリックする
(2) githubの画面となるので、をクリックする
(3) zip形式のactivescaffold-active_scaffold-1acd1ab.zipを、適当な場所にダウンロードする
(4) ダウンロードしたファイルを適当な場所に解凍する
(5) 解凍したファイルを/vender/plugins/フォルダにコピーしactive_scaffoldと名前を変更する
1-4.リソースファイルの生成
(1) 日本語のリソースファイルの生成
D:\Rails_Projects\Appli008>ruby script/generate i18n ja
debug updating environment.rb ...
debug fetching ja.yml fro rails-i18n repository...
exists config/locales
update config/environment.rb
create config/locales/ja.yml
debug 1 models found.
debug 0 translat on keys found in v ews.
debug translating activerecord.models.address ...
debug translating activerecord.attributes.address.namae ...
debug translating activerecord.attributes.address.yubin ...
debug translating activerecord.attributes.address.jusho ...
debug translating activerecord.attributes.address.denwa ...
failed to translate “address” into “ja” language.
debug took 1.438 secs to translate.
create config/locales/translation_ja.yml
(2) translation_ja.ymlの更新
生成された字句を修正し、モデルに関する日本語化を完成させます。
ja:
activerecord:
models:
address: "住所録"attributes:
address:
namae: "名前"
yubin: "郵便番号"
jusho: "住所"
denwa: "電話"
(3) layoutファイルの作成
<!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="ja" lang="ja">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8"/>
<title>Appli008</title>
<%= javascript_include_tag :defaults %>
<%= active_scaffold_includes %>
</head>
<body><%= yield %></body>
</html>
(4) addressesコントローラおよびヘルパーの作成
NetBeansで[生成..]を選択する
ジェネレータ(G): controller
名前(N): addresses
exists app/controllers/
exists app/helpers/
create app/views/addresses
exists test/functional/
create test/unit/helpers/
create app/controllers/addresses_controller.rb
create test/functional/addresses_controller_test.rb
create app/helpers/addresses_helper.rb
create test/unit/helpers/addresses_helper_test.rb
(5) addressesコントローラの修正
ActiveScaffoldは、controller.rbでさまざまな条件を設定します。
class AddressesController < ApplicationController
layout 'activescaffold'active_scaffold :address do |config|
config.columns = [:namae, :yubin, :jusho, :denwa, :created_at]
config.show.columns = [:id, :namae, :yubin, :jusho, :denwa, :created_at, :updated_at]
config.list.per_page = 10
config.list.sorting = [:yubin => :ASC]
endend
config.actions.exclude | 不要なアクションを設定 指定できるアクションは:list一覧、:show参照、:update修正、 :delete削除、:create新規追加、:search検索 (使用例)config.actions.exclude :update, :delete |
config.columns | すべてのアクションに対し表示するカラムを設定 |
config.show.columns | Showアクションに対し表示するカラムを設定 |
config.list.per_page | Listアクションのときの表示行数を設定 |
config.list.sorting | Listアクションのときのデフォルトの表示順序を設定 |
(6) addressesヘルパーの修正
表示内容を変更する場合は、ヘルパーにメソッドを追加します。ここでは日付をyyyymmdd形式に変更しています。
module AddressesHelper
def created_at_column(record)
record[:created_at].strftime("%Y-%m-%d")
end
def updated_at_column(record)
record[:updated_at].strftime("%Y-%m-%d")
enddef created_at_form_column(record, name)
input(:record, :created_at, :name=>name, :order=>[:year,:month,:day], :use_month_numbers=>true)
end
def updated_at_form_column(record, name)
input(:record, :updated_at, :name=>name, :order=>[:year,:month,:day], :use_month_numbers=>true)
end
end
(8) 日本語化されていない項目の追加
idやcreated_atなどモデルの日本語化で定義していなかった項目を追加します。
ja:
activerecord:
models:
address: "住所録"attributes:
address:
id: "ID"
namae: "名前"
yubin: "郵便番号"
jusho: "住所"
denwa: "電話"
created_at: "作成日"
updated_at: "更新日"
1-5.検証機能の追加
(1) モデルにvalidationを追加します。
class Address < ActiveRecord::Base
validates_presence_of :namae
validates_presence_of :yubin
validates_presence_of :jusho
validates_presence_of :denwa
end