In the last content, we created the order model, and the presentation page, this time continue to write checkout processing.
1. On the checkout.rhtml page, there is a checkout button, the last time you have not written it processing code, now add the Save_order method in Store_controller, the code is as follows:
def save_order
@cart = find_cart
@order = order.new (Params[:order])
@order. Line_items <<@ Cart.items
if @order. Save
@cart. empty!
Redirect_to_index (' Thank for your order ')
else
render (:action=> ' checkout ') end
After adding the above code, click the Checkout button and return to the index page.
2. Of course, on the checkout page, we usually don't let the name and other information be empty, so let's add an entry check to the page, as mentioned in the fifth essay in the previous series. Here we modify the Order.rb file and add a code:
Validates_presence_of:name,: Email,: Address,:p Ay_type
Also modify the checkout.rhtml file so that the error message is displayed:
<%= error_messages_for ("order")%> <%= Stylesheet_link_tag "scaffold",
"Depot": Media => "All"%>
At this point, if the value of the item is not filled in, an error message is displayed, as shown in the figure: