Ruby on rails development from scratch (windows) (12)-Order (Order)

Source: Internet
Author: User
Tags ruby on rails

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.