----------------------------
Step 1 アプリケーション新規
----------------------------
① >rails shop -d postgresql
② >cd shop
③ >createuser -a -d -U postgres -P shop
④ >ruby script/generate scaffold Area name:string
⑤ >ruby script/generate scaffold Shop shop:string area_id:integer
⑥ >rake db:create:all
⑦ >rake db:migrate
③実行する結果:
'create' は、内部コマンドまたは外部コマンド、操作可能なプログラムまたはバッチ ファイルとして認識されていません。
①実行すと後、shop/config/database.ymlは以下のようになっています。
development:
adapter: postgresql
encoding: unicode
database: shop_development
pool: 5
username: shop
password:
usernameとpasswordはPostgresqlのユーザ名とパスワードに修正する必要です。
⑦実行する結果:
== CreateAreas: migrating ====================================================
-- create_table(:areas)
-> 0.1720s
== CreateAreas: migrated (0.1720s) ===========================================
== CreateShops: migrating ====================================================
-- create_table(:shops)
-> 0.1400s
== CreateShops: migrated (0.1400s) ===========================================
pgAdminⅢを開いて、shop_developmentというデータベースの下に三つのテーブルが作られました。
areas
id serial NOT NULL,
"name" character varying(255),
created_at timestamp without time zone,
updated_at timestamp without time zone,
schema_migrations
shops
id serial NOT NULL,
shop character varying(255),
area_id integer,
created_at timestamp without time zone,
updated_at timestamp without time zone,
Appフォルダー構造は以下のようです。
app
-controllers
-application_controller.rb
-areas_controller.rb
-shops_controller.rb
-helpers
-application_helper.rb
-areas_helper.rb
-shops_helper.rb
-models
-area.rb
-shop.rb
-views
-areas
-edit.html.erb
-index.html.erb
-new.html.erb
-show.html.erb
-layouts
-areas.html.erb
-shops.html.erb
-shops
-edit.html.erb
-index.html.erb
-new.html.erb
-show.html.erb
----------------------------
Step 2 動作を確認
----------------------------
① >ruby script/server
http://localhost:3000/にアクセスして、「About your application’s environment」をクッリクして、以下の情報が表示させます。
http://localhost:3000/areasにアクセスして、area情報の一覧、登録、編集、削除という機能ができています。
http://localhost:3000/shopsにアクセスして、shop情報の一覧、登録、編集、削除という機能ができています。
そして、areaフィールドを追加します。以下のようです。
「極端にいえば、プログラマがRubyのコードを1行も書くことなく、アプリケーションの開発が可能なのである。」と言われていますが、確かにそうですね。。。。
この以上の①④⑤⑥⑦五つの文で、DB操作するアプリケーションが作られて、DBのテーブルも作られました。
----------------------------
Step 3 ソース編集
----------------------------
Shopを追加するとき、Areaの入力フィールドがAreaのidに関連していますので、地域を選択できるようにします。
① 新規画面:shop/app/views/shops/new.html.erbを以下のように修正します。
<%= f.text_field :area_id %>
⇒<%= select("shop","area_id",Area.find(:all).collect{|p|[p.name, p.id]}) %>
② 詳細画面:shop/app/views/shops/show.html.erbを以下のように修正します。
<%=h @shop.area_id %>
⇒<%=h Area.find(@shop.area_id).name %>
③ 一覧画面:shop/app/views/shops/index.html.erbを以下のように修正します。
<td><%=h shop.area_id %></td>
⇒<td><%=h Area.find(shop.area_id).name %></td>
④ 編集画面:shop/app/views/shops/edit.html.erbを以下のように修正します。
<%= f.text_field :area_id %>
⇒<%= select("shop","area_id",Area.find(:all).collect{|p|[p.name,p.id]}) %>
修正した後、http://localhost:3000/shopsにアクセスします。
shopの登録画面に、以下のようになっています。
----------------------------
Step 4 ヘルプ定義
----------------------------
ソースコードをきれいにするため、
① Helperで以下のように定義します。
module ShopsHelper
def area_list
Area.find(:all).collect {|p|[p.name, p.id]}
end
end
こうしたら、さっき編集した新規画面と編集画面の「Area.find(:all).collect {|p|[p.name, p.id]}」部分を「area_list」に変更します。
② テーブルの関連付け(belongs_to)をします。
shop/app/models/shop.rbの中に、以下のように書きます。
class Shop < ActiveRecord::Base
belongs_to :area
end
shop/app/models/area.rbの中に、以下のように書きます。
class Area < ActiveRecord::Base
has_many :shops
end
③ テーブルの関連付けで、詳細画面と一覧画面は以下のように修正します。
詳細画面:shop/app/views/shops/show.html.erb
<%=h Area.find(@shop.area_id).name %>
⇒<%=h @shop.area.name %>
一覧画面:shop/app/views/shops/index.html.erb
<td><%=h Area.find(shop.area_id).name %></td>
⇒<td><%=h shop.area.name %></td>
----------------------------
Step 5 バリデーション
----------------------------
地域の名前とお店の名前は入力必須にするため、以下のようなチェック追加します。
① Areaモデル:shop/app/models/area.rbに以下のように追加します。
validates_presence_of:name,:message => 'は必須入力です。'
② Shopモデル:shop/app/models/shop.rbに以下のように追加します。
validates_presence_of:shop,:message => 'は必須入力です。'
最後、http://localhost:3000/shopsとhttp://localhost:3000/areasにアクセスして、
動きを試して見ます。
----------------------------
メモ:
----------------------------
1. scaffold
scaffold(足場)というアプリケーションの土台で、テーブル単位のCRUD(Create, Read, Update, Delete)マスタメンテを自動生成してくれる。
2. テーブル間の結びつき/Associations(関連性)
has_one
has_many
belongs_to
has_and_belongs_to_many
ここまでは「RubyonRails-PostgreSQL.pdf」を参照していました。