我正在为 RoR 中的一个网站编写一个购物车系统,但我的购物车有一个奇怪的问题。
我已经用相同的购物车模型制作了另一个应用程序并且没有这个问题。
所以情况如下:
- 将产品添加到我的购物车,没关系。
- 写cart.total_item,没关系。
- 写 cart.total_price,这个失败。
我的应用程序告诉我我试图用来计算“ cart.total_price ”的“方法”是“未定义”。
这是记录:
显示/var/rails/caveSACMS/app/views/catalogues/index.html.erb其中第 196 行提出:
#<Array:0x00000005ce09f0> 的未定义方法“prod_pxuttc”
提取的源代码(在 #196 行附近):
193:</tr> 194:<tr 风格=“背景颜色:#2c2c2c;”> 195: <th style="text-align:center;padding:2px;color:#a2a2a2;"> 196:总计:<%=@cart.total_price%> 197:</th> 198:</tr> 199:<tr 风格=“背景颜色:#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/instrument.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/instrument.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' 旅程 (1.0.3) lib/journey/router.rb:68:in `block in call' 旅程 (1.0.3) lib/journey/router.rb:56:in `each' 旅程 (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' 机架 (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 `服务' /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'
要求
参数:
{"区域"=>"2", “类别”=>“-1”, "millsim"=>"-1", "price_from"=>"", "price_to"=>"-1", "cart_action"=>"-1", "to_add"=>"-1", “show_cart”=>“-1”}
显示会话转储
_csrf_token: "kDD3qIZpvZInGWKclqSUQoyraIkcs20VKbhBMBZezJ4=" 购物车:#<购物车:0x00000005ce0a68 @items=[[#<CsaV3Prd prod_wid:6024,prod_cid:907509,prod_pxuttc:#<BigDecimal:5ce07e8,'0.36E2',9(18)>>]],@total_price=0> session_id:“7efaefcb7af5aa99d68c7466c1e7ee4c”
在我的控制器中,我根据需要有我的购物车“初始化”。
类目录控制器 < 应用控制器 定义索引 @cart = find_cart @items = @cart.items 结尾 结尾
这是我的购物车模型:
类购物车 attr_reader :项目 定义初始化 @项目 = [] @total_price = 0 结尾 def add_product(产品) @项目 << 产品 产品 结尾 def total_price @items.sum{ |项目| item.prod_pxuttc} 结尾 def total_items @items.length 结尾 结尾
我不明白为什么在这个应用程序上的操作“总价”不起作用。我的其他应用程序在相同型号下工作正常。
这两个应用程序之间的唯一区别是:
- 在 Not-Working 应用程序中,商品价格的列是 ' decimal(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”})
如果有人知道如何解决这个问题,或者如果已经有这个问题的线程(我搜索并没有找到)请告诉我。
谢谢。
编辑 29-05-2012
解决
所以问题是关于选择一个数组。
通过选择一件物品放入购物车,一切正常。
我将参数':all'替换为':first'。
感谢 Frederick Cheung 的回答。