First you create a table with the correct headings, then iterate over the
cart you prepared in the action, printing the product name, how many of that product, the
price of a single product, and the combined price for that row. Last, you print a total price
and a link to check out the products.
Checking Out
The last thing last. People would probably want to be able to order items, so you??™ll allow just
that by adding two new actions to the store controller:
def checkout
menu
@customer = Customer.new
end
def order
CHAPTER 5 ?– A DATABASE-DRIVEN SHOP 82
if params[:customer][:billing_address_zip].blank?
['street','postal','zip','country'].each do |v|
params[:customer]["billing_address_#{v}"] =
params[:customer]["shipping_address_#{v}"]
end
end
cust = Customer.create(params[:customer])
order = Order.create(:customer => cust,
:time => Time.now, :status => 'placed')
canon_cart.each do |k,v|
order.order_lines.create(:product => Product.find(k), :amount => v)
end
session[:cart] = []
session[:price] = 0
flash[:notice] = "Order placed"
redirect_to :action => :index
end
The checkout method is an exercise in simplicity. You call the menu method and create a
new customer from scratch. (The new method on model objects doesn??™t add that model object
to the database. That??™s the main difference between new and create for ActiveRecord models.)
The order method is a little more involved. The big complication is caused by something
you??™ll add to the checkout.
Pages:
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162