In the previous content, we created the order Model and the representation page. This time, we will continue to compile the CHECKOUT process.
1. On the checkout. rhtml page, there is a CHECKOUT button that has not been written for it last time. Now add the save_order method to 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 you for your Order ')
Else
Render (: action => 'checkout ')
End
End
After the above Code is added, click the CHECKOUT button to return to the index page.
2. of course, on the Checkout page, we usually do not leave the Name and other information blank. Next we will add an input check to the page, this is mentioned in the fifth article in the previous series. Here we modify the order. rb file and add the following code:
Validates_presence_of: name,: email,: address,: pay_type
Modify the checkout. rhtml file to display the error prompt:
<% = Error_messages_for ("order") %>
<% = Stylesheet_link_tag "scaffold", "depot",: media => "all" %>
At this time, if the project value is not entered, an error message is displayed,
3. Now we fill in the values for all input projects and click the CHECKOUT button. The page will be migrated to the index page,
Now, our order page has been completed. You can use phpMyAdmin to check the data, order and line_items.
The table already has data.
In addition, click the Show my cart link to see that the shopping cart has been cleared after the order is placed.