天天看點

ruby on rails(10) --- 訂單處理

訂單處理,首先要有一個訂單的詳細清單(包括數量,價格啥的)和一個關于個人的一些資訊的一個表。是以我們建立兩個模型,line_item(清單項),order(清單),然後編輯如下

/db/migrate xxx_create_order xxx_line_item

[color=red]t.integer :product_id, :null => false,:options =>

"CONSTRAINT fk_line_items_products REFERENCES products(id)"

,對product_id進行限制,其範圍隻能在products 表的id中選[/color]

資料庫中兩者的關系已經清楚,接下來在rails中建立關系

這樣三者就可以互相引用。

關聯資料

[color=red]form_for 可以使view 通過controller中與model對應的資料,這樣分析對麼[/color]

[color=red]#:order 對應controller中的@order,而下面的參數如name取得則是model裡面的[/color]

困了,後面就貼代碼

store_controller

order.rb

[color=red]line_items << li因為用到上面的has_many[/color]

/models/line_items.rb

時間不早了,11.36了,洗洗睡吧