我正在为RoR中的一个网站编写一个购物车系统,我的购物车出现了一个奇怪的问题。
我已经使用相同的购物车型号制作了另一款应用,但没有出现此问题。
情况就是这样:
我的应用告诉我'方法'我试图用来计算' cart.total _ 价格'是'< EM>未定义”。
以下是记录:
显示 /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: <%=@cart.total_price%> 197: </th> 198: </tr> 199: <tr style="background-color:#2c2c2c;">
Rails.root:/ var / rails / 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"
在我的控制器中,根据需要我的购物车'初始化'。
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
我不明白为什么在此应用中,总计 _ 价格'的操作不起作用。我的其他应用程序使用相同的模型工作正常。
两个应用程序之间的唯一区别是:
最奇怪的是,我可以将表中的'* 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"})
如果有人知道如何解决问题,或者已经存在针对此问题的线程(我搜索过但未找到)请告诉我。
感谢。
编辑29-05-2012
解决
所以问题在于选择一个数组。
选择一个项目放入购物车,一切正常。
我用'替换参数':all':first'。
感谢Frederick Cheung的回答。
答案 0 :(得分:0)
product = CsaV3Prd.find(:all,{:select=>["prod_wid, prod_cid, prod_pxuttc"],:conditions=>["prod_cid = ?","#{params[:to_add]}"],:order=>"prod_cid DESC"})
返回一个数组(可能包含一个元素),而不是单个产品。如果您只想要一个项目,请使用find :first
。
你也可以从你提供的会话转储中看到这一点,你可以看到@items是一个数组数组(看看它是如何从[[?)
开始的与您的问题无关,但
:select=>["prod_wid, prod_cid, prod_pxuttc"]
工作可能是一个意外(由于Array#to_s的作用),你可能应该做的是
:select=>"prod_wid, prod_cid, prod_pxuttc"