我正在为Rails网上商店制定结帐例程。一切正常,但是在完成教程之后,我进行了一次验证,该验证抛出了SyntaxError syntax error, unexpected tIDENTIFIER, expecting kEND):
错误。
取消注释后,一切正常,但是我仍然想在此进行验证。
该错误与控制器中的第17、46和47行以及模型中的第10行有关。
我的控制器中的相对动作是:
class CheckoutController < ApplicationController
def place_order
@page_title = "Checkout"
@order = Order.new(params[:order])
@order.customer_ip = request.remote_ip
populate_order ### LINE 17
...
end
private
def populate_order
@cart.cart_items.each do |cart_item|
order_item = OrderItem.new(:product_id => cart_item.product_id, :price => cart_item.price, :amount => cart_item.amount) ### LINE 46
@order.order_items << order_item ### LINE 47
end
end
end
order_item模型为:
class OrderItem < ActiveRecord::Base
attr_accessible :amount, :price, :product_id, :order_id
belongs_to :order
belongs_to :product
def validate
errors.add(:amount, "should be one or more") unless amount.nil? || amount > 0 ### LINE 10
errors.add(:price, "should be a positive number") unless price.nil? || price > 0.0
end
end
甚至stackoverflow也无法让我正确地输入这一行
第10行的错误消息和第10行未通过的错误消息如下
Started POST "/checkout/place_order" for 127.0.0.1 at Tue Dec 11 08:03:05 +0100 2018
Processing by CheckoutController#place_order as HTML
Parameters: {"order"=>{"email"=>"test@example.tld", "ship_to_last_name"=>"Smith", "phone_number"=>"123451234134", "ship_to_first_name"=>"John", "ship_to_country"=>"United States of America", "ship_to_postal_code"=>"12345", "ship_to_address"=>"Somewhere Avenue", "ship_to_city"=>"Nowheretorn"}, "utf8"=>"✓", "authenticity_token"=>"xxxxx=", "commit"=>"Place Order"}
Cart Load (0.4ms) SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = ? LIMIT 1 [["id", 3]]
CartItem Load (0.4ms) SELECT `cart_items`.* FROM `cart_items` WHERE `cart_items`.`cart_id` = 3
Completed 500 Internal Server Error in 5ms
SyntaxError (/Users/devaccount/Development/REPRO/webapp/app/models/order_item.rb:10: syntax error, unexpected tIDENTIFIER, expecting kEND):
app/controllers/checkout_controller.rb:46:in `populate_order'
app/controllers/checkout_controller.rb:45:in `populate_order'
app/controllers/checkout_controller.rb:17:in `place_order'
Started POST "/checkout/place_order" for 127.0.0.1 at Tue Dec 11 08:03:29 +0100 2018
Processing by CheckoutController#place_order as HTML
Parameters: {"order"=>{"email"=>"test@example.tld", "ship_to_last_name"=>"Smith", "phone_number"=>"123451234134", "ship_to_first_name"=>"John", "ship_to_country"=>"United States of America", "ship_to_postal_code"=>"12345", "ship_to_address"=>"Somewhere Avenue", "ship_to_city"=>"Nowheretorn"}, "utf8"=>"✓", "authenticity_token"=>"xxxxx=", "commit"=>"Place Order"}
Cart Load (0.2ms) SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = ? LIMIT 1 [["id", 3]]
CartItem Load (0.4ms) SELECT `cart_items`.* FROM `cart_items` WHERE `cart_items`.`cart_id` = 3
SQL (0.1ms) BEGIN
SQL (1.4ms) INSERT INTO `orders` (`created_at`, `customer_ip`, `email`, `error_message`, `phone_number`, `ship_to_address`, `ship_to_city`, `ship_to_country`, `ship_to_first_name`, `ship_to_last_name`, `ship_to_postal_code`, `status`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Tue, 11 Dec 2018 07:03:29 UTC +00:00], ["customer_ip", "127.0.0.1"], ["email", "test@example.tld"], ["error_message", nil], ["phone_number", "123451234134"], ["ship_to_address", "Somewhere Avenue"], ["ship_to_city", "Nowheretorn"], ["ship_to_country", "United States of America"], ["ship_to_first_name", "John"], ["ship_to_last_name", "Smith"], ["ship_to_postal_code", "12345"], ["status", "open"], ["updated_at", Tue, 11 Dec 2018 07:03:29 UTC +00:00]]
SQL (1.1ms) INSERT INTO `order_items` (`amount`, `created_at`, `order_id`, `price`, `product_id`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?) [["amount", 1], ["created_at", Tue, 11 Dec 2018 07:03:29 UTC +00:00], ["order_id", 7], ["price", 10], ["product_id", 13], ["updated_at", Tue, 11 Dec 2018 07:03:29 UTC +00:00]]
(0.5ms) COMMIT
SQL (0.1ms) BEGIN
(0.3ms) UPDATE `orders` SET `updated_at` = '2018-12-11 07:03:29', `status` = 'processed' WHERE `orders`.`id` = 7
(0.4ms) COMMIT
SQL (0.2ms) BEGIN
SQL (0.3ms) DELETE FROM `cart_items` WHERE `cart_items`.`id` = ? [["id", 11]]
(0.4ms) COMMIT
Redirected to http://localhost:3000/checkout/thank_you
Completed 302 Found in 138ms (ActiveRecord: 9.1ms)
我检查了:金额在order_items表中具有整数值,并且在两种情况下都始终大于0。 我希望有人能指出我正确的方向。