レイルに乗ってみた〜 目次
書籍『Rails of Ruby on Rails ~Case of LOCUSANDWONDERS.COM~』で勉強してみた(P157〜)
Orderモデル作成
G5:~/work/locus username$ script/generate model order exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/order.rb create test/unit/order_test.rb create test/fixtures/orders.yml exists db/migrate create db/migrate/20080919060805_create_orders.rb Loaded suite script/generate Started Finished in 0.000738 seconds. 0 tests, 0 assertions, 0 failures, 0 errors
OrderItemモデル作成
G5:~/work/locus username$ script/generate model order_item exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/order_item.rb create test/unit/order_item_test.rb create test/fixtures/order_items.yml exists db/migrate create db/migrate/20080919061426_create_order_items.rb Loaded suite script/generate Started Finished in 0.001182 seconds. 0 tests, 0 assertions, 0 failures, 0 errors
マイグレーションファイル修正
/work/locus/db/migrate/20080919060805_create_orders.rb
書籍通りなので略。
P158の「012_create_orders.rb」は「012_create_order_items.rb」のことかな?
/work/locus/db/migrate/20080919061426_create_order_items.rb
内容は書籍通りなので略。
マイグレーション実行
G5:~/work/locus username$ rake db:migrate (in /Users/username/work/locus) == 20080919060805 CreateOrders: migrating ===================================== -- create_table(:orders) -> 0.0698s == 20080919060805 CreateOrders: migrated (0.0711s) ============================ == 20080919061426 CreateOrderItems: migrating ================================= -- create_table(:order_items) -> 0.0230s == 20080919061426 CreateOrderItems: migrated (0.0240s) ======================== Loaded suite /opt/local/bin/rake Started Finished in 0.000952 seconds. 0 tests, 0 assertions, 0 failures, 0 errors
Orderモデルの修正
次のファルを書き換え。
/work/locus/app/models/order.rb
/work/locus/app/models/order_item.rb
書籍通りなので略。
書き込み量が多くなってきたなぁ(||゚Д゚)
決済・注文画面の作成 Orderコントローラを作成
G5:~/work/locus username$ script/generate controller order new confirm create exists app/controllers/ exists app/helpers/ create app/views/order exists test/functional/ create app/controllers/order_controller.rb create test/functional/order_controller_test.rb create app/helpers/order_helper.rb create app/views/order/new.html.erb create app/views/order/confirm.html.erb create app/views/order/create.html.erb Loaded suite script/generate Started Finished in 0.003514 seconds. 0 tests, 0 assertions, 0 failures, 0 errors
次のファイルを書き換え。
/work/locus/app/controllers/order_controller.rb
書籍通りなので略。
ちなみに サンプル使う場合は、def createの下記のところをとりあえずコメントアウト。
def create @order = Order.new(params[:order]) cart_to_order if params[:btn_cancel] render :action => 'new' elsif @order.save @cart.cart_items.destroy_all # logger.info "EMAIL:" + @order.email # OrderMailer.deliver_order(@order) else render :action => 'new' end end
お客様情報入力ページ
書き換え
/work/locus/app/views/order/new.html.erb
書籍通りなので略。
注文内容確認ページ
書き換え
/work/locus/app/views/order/confirm.html.erb
書籍通りなので略。
注文完了ページ
書き換え
/work/locus/app/views/order/create.html.erb
書籍通りなので略。
ブラウザで確認
http://localhost:3000/
カートに商品を入れて「カートの中を表示」をクリック。
入力して「確認する」クリック。
「注文する」ボタンを押すと
よし(・∀・)b
返信メールは次回。