total_price
cart
中的所有產品都沒有保存,因爲它應該是。它總是顯示1,不管總價格是多少。在安裝money gem和google_currency gem之後,出現了這個問題,所以我可以根據所選的區域設置更改產品的價格本身,而不僅僅是貨幣。虛擬值total_price被保存爲1而不是總價格編號
所以總價總是1,但是同時我在購物過程中屏幕上顯示的金額數是正確的,我的意思是如果我選擇價格爲A和B的兩種產品,那麼在購物車(在屏幕上)我擁有合適的total_price = A + B ..比這更多的時候,當我在填寫信用卡字段後按下創建訂單時,ActiveMerchant將數據發送到paypal和USD進入PayPal是TOTAL_PRICE,我在車A + B ..
問題是:爲什麼應用程序需要正確的total_price
並將其發送給貝寶,但在同一時間不能存儲此total_price
在訂單上的數據庫創建,它節省了1英寸代替。
什麼控制檯說,當我創建爲了:
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO `orders` (`card_expires_on`, `card_type`, `cart_id`, `created_at`, `first_name`, `ip_address`, `last_name`, `total`, `updated_at`, `user_id`) VALUES ('2012-04-01', 'visa', 5, '2012-04-18 21:35:38', 'Rosca', '127.0.0.1', 'Sergiu', 1, '2012-04-18 21:35:38', 7)
order_transaction細節:
SQL (0.5ms) INSERT INTO `order_transactions` (`action`, `amount`, `authorization`, `created_at`, `message`, `order_id`, `params`, `success`, `updated_at`) VALUES ('purchase', 1, '0GT41652PP785722H', '2012-04-18 21:35:46', 'Success', 5, '--- \nbuild: \"2764190\"\nAck: Success\ntimestamp: \"2012-04-18T21:35:45Z\"\nTransactionID: 0GT41652PP785722H\namount: \"42.00\"\namount_currency_id: USD\ntransaction_id: 0GT41652PP785722H\nack: Success\nBuild: \"2764190\"\navs_code: X\nversion: \"62.0\"\nTimestamp: \"2012-04-18T21:35:45Z\"\nCorrelationID: 690aa904db3c\nAmount: \"42.00\"\nAVSCode: X\nVersion: \"62.0\"\ncvv2_code: M\nCVV2Code: M\ncorrelation_id: 690aa904db3c\n', 1, '2012-04-18 21:35:46')
就如何創建訂單幾個細節:
訂購s_controller.rb
def create
@order = current_cart.build_order(params[:order])
@order.user_id = current_user.id
@order.total = current_cart.total_price
# raise current_cart.total_price.inspect
@order.line_items = current_cart.line_items
@order.ip_address = request.remote_ip
if @order.save
if @order.purchase
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
else
render :action => "failure"
end
respond_to do |format|
format.html { redirect_to products_path, :notice =>
'Thank you for your order.' }
format.json { render :json => @order }
end
else
render :action => 'new'
end
end
注如果我激活提高視察控制器線我得到
#<Money cents:4200 currency:USD>
錯誤,其中4200是在這種情況下,2箇中選擇產品TOTAL_PRICE。 42.00美元。 這是一個更多的證據表明total_price公式可以正常工作,直到應用程序需要將其存儲在數據庫中。
總價:
total_price
在cart.rb和line_item.rb形成
line_item.rb
belongs_to :order
belongs_to :product
belongs_to :cart
belongs_to :user
def total_price
product.price * quantity
end
cart.rb
has_many :line_items#, :dependent => :destroy
has_one :order
def to_s
id
end
def add_product(product_id)
current_item = line_items.find_by_product_id(product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(:product_id => product_id)
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end
編輯
在數據庫中的order.total
字段類型現在是整數(11),但我試過小數(10,0)來存儲美分的價格,並沒有工作,無論如何,我總是得到1美元的總價。 products
的價格是十進制(10,0)。
真的很大謝謝幫助..可能對未來的開發人員有用。
您可能需要的任何其他信息,請讓我知道。