Ruby on Rails development from scratch (Windows) (12)-Orders (order)

Source: Internet
Author: User
Tags empty ruby on rails

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:

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.