TAILIEUCHUNG - Agile Web Development with Rails phần 3

Phương pháp này có tới 1. Capture giá trị từ biểu mẫu để cư trú một đối tượng đặt hàng mô hình mới. 2. Thêm mục hàng giỏ hàng của chúng tôi được ra lệnh. 3. Xác nhận và lưu theo thứ tự. Nếu điều này không, hiển thị các thông báo thích hợp và cho phép người dùng sửa chữa bất kỳ vấn đề. 4. Một khi lệnh được lưu thành công, hiển thị lại các trang danh mục, bao gồm một thông báo xác nhận rằng đơn đặt hàng đã được đặt. Phương pháp này kết thúc lên. | Iteration D1 Capturing an Order 101 This method has to 1. Capture the values from the form to populate a new Order model object. 2. Add the line items from our cart to that order. File 30 3. Validate and save the order. If this fails display the appropriate messages and let the user correct any problems. 4. Once the order is successfully saved redisplay the catalog page including a message confirming that the order has been placed. The method ends up looking something like this. Line 1 def save_order @cart find_cart @order params order @ @ 5 if @ @ redirect_to_index Thank you for your order. else render action checkout 10 end end On line 3 we create a new Order object and initialize it from the form data. In this case we want all the form data related to order objects so we select the order hash from the parameters we ll talk about how forms are linked to models on page 341 . The next line adds into this order the line items that are already stored in the cart the session data is still there throughout this latest action. Notice that we didn t have to do anything special with the various foreign key fields such as setting the order_id column in the line item rows to reference the newly created order row. Rails does that knitting for us using the has_many and belongs_to declarations we added to the Order and Lineitem models. Next on line 5 we tell the order object to save itself and its children the line items to the database. Along the way the order object will perform validation but we ll get to that in a minute . If the save succeeds we empty out the cart ready for the next order and redisplay the catalog using our redirect_to_index method to display a cheerful message. If instead the save fails we redisplay the checkout form. One last thing before we call our customer over. Remember when we showed her the first product maintenance page She asked us to add validation. We should probably do that for our .

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.