I'm programming a cart system for one website in RoR and I have a strange problem with my cart.
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.
- write cart.total_price, this fail.
My app tell me the 'method' I'm trying to use to calculate 'cart.total_ price' is 'undefined'.
Here is the record:
Showing /var/rails/caveSACMS/app/views/catalogues/index.html.erb where line #196 raised:
undefined method `prod_pxuttc' for #<Array:0x00000005ce09f0>
Extracted source (around line #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/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.
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.
- on the Working app, the column for the item price is 'int(11)' type.
And the most strange thing is that I can sort and render the column '*prod_pxuttc*' from the table into a listing. 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
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'.
Thanks to Frederick Cheung for his answer.