ruby on rails(10) --- 订单处理

订单处理,首先要有一个订单的详细列表(包括数量,价格啥的)和一个关于个人的一些信息的一个表。因此我们创建两个模型,line_item(列表项),order(列表),然后编辑如下
/db/migrate xxx_create_order xxx_line_item

class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.string :name
t.text :address
t.string :email
t.string :pay_type, :limit => 10
t.timestamps
end
end

def self.down
drop_table :orders
end
end


class CreateLineItems < ActiveRecord::Migration
def self.up
create_table :line_items do |t|
t.integer :product_id, :null => false,:options =>
"CONSTRAINT fk_line_items_products REFERENCES products(id)"
t.integer :order_id,:null => false, :options =>
"CONSTRAINT fk_line_items_orders ReEFERENCES orders(id)"
t.integer :quantity, :null => false
t.decimal :total_price, :null => false,:precision => 8,:scale => 2
t.timestamps
end
end

def self.down
drop_table :line_items
end
end

[color=red]t.integer :product_id, :null => false,:options =>
"CONSTRAINT fk_line_items_products REFERENCES products(id)"
,对product_id进行约束,其范围只能在products 表的id中选[/color]
数据库中两者的关系已经清楚,接下来在rails中建立关系

class Order < ActiveRecord::Base
has_many :line_items #包含子集

class LineItem < ActiveRecord::Base
belongs_to :order #有外键就用belogns_to,表示LineItem是这两者的
belongs_to :product #子集,只有父存在,只才存在。

这样三者就可以相互引用。

放置一个checkout 按钮,然后定义其方法
<%=button_to "Checkout",:action => :checkout%> ----_cart.html.erb
/app/controller/store_controller.rb
def checkout
@cart = find_cart
if @cart.items.empty?
rediret_to_index("Your cart is empty")
else
@order = Order.new
end
end
#如果购物车里有东西,则生成一个订单

关联数据
[color=red]form_for 可以使view 通过controller中与model对应的数据,这样分析对么[/color]

<div id="depot-form">
<%= error_messages_for "order"%>
<fieldset>
<legend>Please Enter Your Details</legend>

<%form_for :order, :url => {:action => :save_order} do |f|%>

<p>
<%=label :order,:name,"Name:"%>
<%= f.text_field :name,:size => 40%>
</p>
<p>
<%= label :order,:pay_type, "Pay_with:"%>
<%= f.select :pay_type, Order::PAYMENT_TYPES,
:promt => "Select a payment type"
%>
</p>
<%= submit_tag "Place Order",:class => "submit" %>
<%end%>
</fieldset>
</div>

[color=red]#:order 对应controller中的@order,而下面的参数如name取得则是model里面的[/color]
困了,后面就贴代码

/model/order

PAYMENT_TYPES = [
#Display Store in db
["Check","check"],
["Credit Cart","cc"],
["Purchase Order","po"]
]

validates_presence_of :name,:address,:email,:pay_type
validates_inclusion_of :pay_type,:in => PAYMENT_TYPES.map{|dispay,value|value}

store_controller

def save_order
@cart = find_cart
@order = Order.new(params[:order])
@order.add_line_items_from_cart(@cart)
if @order.save #save哪里来的
session[:cart] = nil
redirect_to_index("Thank you for your order")
else
render :action => :checkout
end
end

order.rb

def add_line_items_from_cart(cart)
cart.items.each do |item|
li = LineItem.from_cart_item(item)
line_items << li
end
end

[color=red]line_items << li因为用到上面的has_many[/color]
/models/line_items.rb

def self.from_cart_item(cart_item)
li = self.new
li.product = cart_item.product
li.quantity = cart_item.quantity
li.total_price = cart_item.price
li
end


时间不早了,11.36了,洗洗睡吧
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值