ruby on rails(10) --- 订单处理
订单处理,首先要有一个订单的详细列表(包括数量,价格啥的)和一个关于个人的一些信息的一个表。因此我们创建两个模型,line_item(列表项),order(列表),然后编辑如下 /db/migrate xxx_create_order xxx_line_item [code="ruby"] 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 [/code] [code="ruby"] 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 [/code] [color=red]t.integer :product_id, :null => false,:options => "CONSTRAINT fk_line_items_products REFERENCES products(id)" ,对product_id进行约束,其范围只能在products 表的id中选[/color] 数据库中两者的关系已经清楚,接下来在rails中建立关系 [code="ruby"] class Order < ActiveRecord::Base has_many :line_items #包含子集 class LineItem < ActiveRecord::Base belongs_to :order #有外键就用belogns_to,表示LineItem是这两者的 belongs_to :product #子集,只有父存在,只才存在。 [/code] 这样三者就可以相互引用。 [code="ruby"] 放置一个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 #如果购物车里有东西,则生成一个订单 [/code] 关联数据 [color=red]form_for 可以使view 通过controller中与model对应的数据,这样分析对么[/color] [code="ruby"]
<%= error_messages_for "order"%>
Please Enter Your Details <%form_for :order, :url => {:action => :save_order} do |f|%>
<%=label :order,:name,"Name:"%> <%= f.text_field :name,:size => 40%>
<%= label :order,:pay_type, "Pay_with:"%> <%= f.select :pay_type, Order::PAYMENT_TYPES, :promt => "Select a payment type" %>
<%= submit_tag "Place Order",:class => "submit" %> <%end%>
[/code] [color=red]#:order 对应controller中的@order,而下面的参数如name取得则是model里面的[/color] 困了,后面就贴代码 [code="ruby"] /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} [/code] store_controller [code="ruby"] 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 [/code] order.rb [code="ruby"] def add_line_items_from_cart(cart) cart.items.each do |item| li = LineItem.from_cart_item(item) line_items << li end end [/code] [color=red]line_items << li因为用到上面的has_many[/color] /models/line_items.rb [code="ruby"] 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 [/code] 时间不早了,11.36了,洗洗睡吧