我正在爲RoR中的一個網站編程購物車系統,並且我的購物車有一個奇怪的問題。購物車陣列麻煩,cart.total_price
我已經使用相同的購物車模型製作了另一個應用程序,但沒有此問題。
因此,這裏的情況:
- 增加產品到我的車,這是確定。
- 寫cart.total_item,沒關係。
- 寫cart.total_price,這失敗。
我的應用程序告訴我 '方法' 我試圖用它來計算 'cart.total _ 價格' 是 '未定義'。
這裏是記錄:
顯示/var/rails/caveSACMS/app/views/catalogues/index.html.erb其中線#196提出:
undefined method `prod_pxuttc' for #<Array:0x00000005ce09f0>
提取的源(圍繞線#196):
193: </tr> 194: <tr style="background-color:#2c2c2c;"> 195: <th style="text-align:center;padding:2px;color:#a2a2a2;"> 196: Total: <%[email protected]_price%> 197: </th> 198: </tr> 199: <tr style="background-color:#2c2c2c;">
Rails.root:在/ var /導軌/對myApp
個app/models/cart.rb:15:in `block in total_price' activesupport (3.2.1) lib/active_support/core_ext/enumerable.rb:60:in `map' activesupport (3.2.1) lib/active_support/core_ext/enumerable.rb:60:in `sum' app/models/cart.rb:15:in `total_price' app/views/catalogues/index.html.erb:196:in `_app_views_catalogues_index_html_erb___2685208389527055012_43230480' actionpack (3.2.1) lib/action_view/template.rb:143:in `block in render' activesupport (3.2.1) lib/active_support/notifications.rb:125:in `instrument' actionpack (3.2.1) lib/action_view/template.rb:141:in `render' actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:41:in `block (2 levels) in render_template' actionpack (3.2.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument' activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument' activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument' actionpack (3.2.1) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument' actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:40:in `block in render_template' actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:48:in `render_with_layout' actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:39:in `render_template' actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:12:in `render' actionpack (3.2.1) lib/action_view/renderer/renderer.rb:36:in `render_template' actionpack (3.2.1) lib/action_view/renderer/renderer.rb:17:in `render' actionpack (3.2.1) lib/abstract_controller/rendering.rb:109:in `_render_template' actionpack (3.2.1) lib/action_controller/metal/streaming.rb:225:in `_render_template' actionpack (3.2.1) lib/abstract_controller/rendering.rb:103:in `render_to_body' actionpack (3.2.1) lib/action_controller/metal/renderers.rb:28:in `render_to_body' actionpack (3.2.1) lib/action_controller/metal/compatibility.rb:50:in `render_to_body' actionpack (3.2.1) lib/abstract_controller/rendering.rb:88:in `render' actionpack (3.2.1) lib/action_controller/metal/rendering.rb:16:in `render' actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render' activesupport (3.2.1) lib/active_support/core_ext/benchmark.rb:5:in `block in ms' /usr/share/ruby-rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:310:in `realtime' activesupport (3.2.1) lib/active_support/core_ext/benchmark.rb:5:in `ms' actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:40:in `block in render' actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime' activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime' actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:39:in `render' actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render' actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action' actionpack (3.2.1) lib/abstract_controller/base.rb:167:in `process_action' actionpack (3.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action' actionpack (3.2.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action' activesupport (3.2.1) lib/active_support/callbacks.rb:414:in `_run__1902602080326961788__process_action__2511143183103068215__callbacks' activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (3.2.1) lib/abstract_controller/callbacks.rb:17:in `process_action' actionpack (3.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action' actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument' activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument' actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action' actionpack (3.2.1) lib/action_controller/metal/params_wrapper.rb:205:in `process_action' activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action' actionpack (3.2.1) lib/abstract_controller/base.rb:121:in `process' actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process' actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch' actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' actionpack (3.2.1) lib/action_controller/metal.rb:246:in `block in action' actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `call' actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `dispatch' actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in `call' journey (1.0.3) lib/journey/router.rb:68:in `block in call' journey (1.0.3) lib/journey/router.rb:56:in `each' journey (1.0.3) lib/journey/router.rb:56:in `call' actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' rack (1.4.1) lib/rack/etag.rb:23:in `call' rack (1.4.1) lib/rack/conditionalget.rb:25:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in `call' rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/cookies.rb:338:in `call' activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call' activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__2985483728309837116__call__1908674296767231568__callbacks' activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app' railties (3.2.1) lib/rails/rack/logger.rb:16:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call' rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' rack (1.4.1) lib/rack/runtime.rb:17:in `call' activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call' rack (1.4.1) lib/rack/lock.rb:15:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call' railties (3.2.1) lib/rails/engine.rb:479:in `call' railties (3.2.1) lib/rails/application.rb:220:in `call' rack (1.4.1) lib/rack/content_length.rb:14:in `call' railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call' rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' /usr/share/ruby-rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' /usr/share/ruby-rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' /usr/share/ruby-rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
請求
參數:
{"region"=>"2", "categorie"=>"-1", "millesim"=>"-1", "price_from"=>"", "price_to"=>"-1", "cart_action"=>"-1", "to_add"=>"-1", "show_cart"=>"-1"}
顯示會話轉儲
_csrf_token: "kDD3qIZpvZInGWKclqSUQoyraIkcs20VKbhBMBZezJ4=" cart: #<Cart:0x00000005ce0a68 @items=[[#<CsaV3Prd prod_wid: 6024, prod_cid: 907509, prod_pxuttc: #<BigDecimal:5ce07e8,'0.36E2',9(18)>>]], @total_price=0> session_id: "7efaefcb7af5aa99d68c7466c1e7ee4c"
在我的控制器我有我的車'Initializat離子'。
class CataloguesController < ApplicationController def index @cart = find_cart @items = @cart.items end end
這是我的車型號:
class Cart attr_reader :items def initialize @items = [] @total_price = 0 end def add_product(product) @items << product product end def total_price @items.sum{ |item| item.prod_pxuttc} end def total_items @items.length end end
我不明白,爲什麼在這個應用程序中的行動 '總 _ 價格' 不工作。我的其他應用程序適用於相同的模型。
在兩個應用程序之間的唯一區別在於:
- 上的未工作的應用程序,用於該項目的價格的列是「十進制(10,2)」類型。
- 在工作應用程序中,項目價格的列爲'int(11)'類型。
而最奇怪的是,我可以從列表中對列'* prod_pxuttc *'進行排序並呈現爲列表。它呈現了價值。
這裏是兩個型動物的順序排序從表中的值相同:
- 第一種方法(工作和渲染的值):(這個順序排序的許多項目)
@prod = CsaV3Prd.find(:all,{:conditions=>["prod_cat = ?","#{params[:categorie]}"],:order=>"prod_cid DESC"})
- 第二種方法(購物車的方法,不工作,並調用錯誤):(這個命令排序一個獨特的項目)
product = CsaV3Prd.find(:all,{:select=>["prod_wid, prod_cid, prod_pxuttc"],:conditions=>["prod_cid = ?","#{params[:to_add]}"],:order=>"prod_cid DESC"})
如果有人知道如何解決這個問題,或者是否已經有這個麻煩(我搜索並沒有發現)請告訴我一個線程。
謝謝。
編輯2012-05-29
解決
所以這個問題是有關選擇的數組。
通過選擇一個單件放入購物車,所有作品。
我替換了參數':全部'通過':首先'。
感謝Frederick Cheung的回答。
我用我的第一應用相同的方法(除了:選擇只拿需要的信息),我可以毫不費力地使用total_price函數。 – Zyth23
這一切都取決於您對返回值做了什麼 - 如果您將其當作單一產品對待它,它就無法工作,因爲它不是 - 它是一個數組(也請注意,即使您修復此問題,您的購物車仍然包含不良數據 - 您需要將其清除) –
我想從選擇的項目中返回「prod_pxuttc」值。然後,我需要呈現「總價」值(價格值= prod_pxuttc)。但是我不能返回存儲在購物車中的任何東西。我不明白爲什麼我不能,因爲我已經在另一個應用程序上使用過這個購物車,並且在這個其他應用程序中也一樣。 – Zyth23