lundi 10 décembre 2018

Validation not passing for 'SyntaxError syntax error, unexpected tIDENTIFIER, expecting kEND):'

I am working on a checkout routine for a Rails webshop. Everything is working, but following a tutorial I placed a validation which throws a SyntaxError syntax error, unexpected tIDENTIFIER, expecting kEND): error.

When I uncomment it, everything works, but I still would like to implement a validation in this place.

The error is related to lines 17, 46 and 47 in the controller and 10 in the model.

The relative action in my controller is:

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 

The order_item model is:

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

Not even stackoverflow lets me put in this line correctly

The error messages for line 10 and the pass with line 10 uncommented are as follows

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)

I hope someone can point me into the right direction.

Aucun commentaire:

Enregistrer un commentaire