简体   繁体   English

购物车阵列出现问题,cart.total_price

[英]Cart Array trouble, cart.total_price

I'm programming a cart system for one website in RoR and I have a strange problem with my cart. 我正在为RoR中的一个网站编程一个购物车系统,但是我的购物车有一个奇怪的问题。

I already made another app with the same cart model and didn't have this problem. 我已经用相同的购物车型号制作了另一个应用程序,但是没有这个问题。

So here is the situation: 因此,情况如下:

  • add product to my cart, that's ok. 将产品添加到我的购物车中,没关系。
  • write cart.total_item, that's ok. 写cart.total_item,没关系。
  • write cart.total_price, this fail. 写出cart.total_price,失败了。

My app tell me the ' method ' I'm trying to use to calculate ' cart.total _ price ' is ' undefined '. 我的应用程序告诉我“ 方法 ”我试图用它来计算“cart.total _ 价格 ”是“ 未定义 ”。

Here is the record: 这是记录:

Showing /var/rails/caveSACMS/app/views/catalogues/index.html.erb where line #196 raised: 显示/var/rails/caveSACMS/app/views/catalogues/index.html.erb ,其中#196行出现:

undefined method `prod_pxuttc' for #<Array:0x00000005ce09f0>

Extracted source (around line #196): 提取的源(围绕行#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 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'

Request 请求

Parameters: 参数:

{"region"=>"2",
     "categorie"=>"-1",
     "millesim"=>"-1",
     "price_from"=>"",
     "price_to"=>"-1",
     "cart_action"=>"-1",
     "to_add"=>"-1",
     "show_cart"=>"-1"}

Show session dump 显示会话转储

_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"

In my controller I have my cart ' Initialization ' as needed. 在控制器中,根据需要,我的购物车具有“ Initialization ”。

class CataloguesController < ApplicationController
      def index
        @cart = find_cart
        @items = @cart.items
      end
    end

Here is my Cart Model: 这是我的购物车模型:

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

I don't understand why on this app the action ' total _ price ' don't work. 我不明白为什么在此应用程序上执行“ 总计 _ 价格 ”操作无效。 My other app work fine with the same model. 我的其他应用程序在相同模型下也可以正常工作。

The only difference between the two apps is that: 这两个应用之间的唯一区别是:

  • on the Not-Working app, the column for the item price is ' decimal(10,2) ' type. 在“不工作”应用程序上,商品价格的列为十进制(10,2) ”类型。
  • on the Working app, the column for the item price is ' int(11) ' type. 在工作应用程序上,项目价格的列为int(11) ”类型。

And the most strange thing is that I can sort and render the column '*prod_pxuttc*' from the table into a listing. 最奇怪的是,我可以对表中的列'* prod_pxuttc *'进行排序并将其呈现为列表。 It renders the value. 它呈现值。

Here is the two differents order to sort the same value from the table: 这是从表中对相同值进行排序的两种不同顺序:

  • First method (working and rendering the value): (this order sorts many items) 第一种方法(处理和呈现值):( 此顺序对许多项目进行排序)
@prod = CsaV3Prd.find(:all,{:conditions=>["prod_cat = ?","#{params[:categorie]}"],:order=>"prod_cid DESC"})
  • Second method (the cart method that doesn't work and call the error): (this order sort one unique item) 第二种方法(无法使用并调用错误的购物车方法):( 此订单对一个唯一商品进行排序)
product = CsaV3Prd.find(:all,{:select=>["prod_wid, prod_cid, prod_pxuttc"],:conditions=>["prod_cid = ?","#{params[:to_add]}"],:order=>"prod_cid DESC"})

If someone know how to solve the problem, or if there is already a thread for this trouble (I searched and didn't find) please tell me. 如果有人知道如何解决问题,或者已经有解决此问题的线索(我搜索了但没有找到),请告诉我。

Thanks. 谢谢。

EDIT 29-05-2012 编辑2012年5月29日

Resolved 解决

So the problem was about selecting an Array. 因此,问题在于选择数组。

By selecting one single item to put into the cart, all works. 通过选择一个要放入购物车的物品,所有物品都可以工作。

I replaced the parameter ':all' by ':first' . 我将参数':all'替换为':first'

Thanks to Frederick Cheung for his answer. 感谢张学友的回答。

product = CsaV3Prd.find(:all,{:select=>["prod_wid, prod_cid, prod_pxuttc"],:conditions=>["prod_cid = ?","#{params[:to_add]}"],:order=>"prod_cid DESC"})

returns an array (possibly containing one element), not a single product. 返回一个数组(可能包含一个元素),而不是单个乘积。 Use find :first if you only want one item. 如果只需要一项,请使用find :first

You can also see this from the dump of the session you provided, where you can see that @items is an array of array (see how it starts with [[ ? ) 您还可以从您提供的会话的转储中看到这一点,在这里您可以看到@items是一个数组数组(请参阅以[[?

Not related to your problem, but 与您的问题无关,但

:select=>["prod_wid, prod_cid, prod_pxuttc"]

working is probably an accident (due to what Array#to_s does), what you probably should be doing is 工作可能是个意外(由于Array#to_s的原因),您可能应该做的是

:select=>"prod_wid, prod_cid, prod_pxuttc"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将购物车的“总价”属性连接到付款配置 - Connecting a shopping cart “total price” attribute to a payments configuration 购物车价格变动 - shopping cart price change 在navbar Rails应用中显示购物车数量而不是购物车总价 - Showing Number of Cart Items instead of Cart total price in navbar Rails app 如何在 ruby on rails 的购物车总价中增加额外的运费? - How to add an extra shipping cost to the total price of my shopping cart in ruby ​on rails? 在Rails中以Rails方式显示购物车总数 - Displaying cart total in Rails, the Rails way 扩展“仓库”应用程序以包括购物车中的全部物品 - Extending 'depot' app to include total items in cart 如何更新购物车中的产品数量并在滑轨上的红宝石上的购物车页面中显示总金额? - How to update quantity of product in the cart and display total amount in cart page on ruby on rails? PayPal express / Active Merchant - 不显示订单项或购物车总数? - PayPal express / Active Merchant - not displaying line items or cart total? 从狂欢网站上删除价格信息和“添加到购物车” - Removing price information and 'Add to Cart' from spree website Rails - 从 URL 中获取像“cart_item[price]=5%2C70€”这样的参数 - Rails - Get Params like "cart_item[price]=5%2C70€" out of URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM