将商品添加到购物车首次尝试失败

时间:2012-02-21 11:13:40

标签: ruby-on-rails ruby

嗨我正在使用ruby on rails我只是想知道我的add_product是否错误,因为当我将我的产品添加到我的购物车时,首先尝试失败并给我一个

  

无法识别大错误

但点击我的浏览器返回并在第二次添加项目尝试它工作正常,但没有计算我的第一次尝试添加我的产品 这是我的add_product方法

my model / cart.rb

  has_many :line_items, :dependent => :destroy

  def add_product(product_id)
    current_item = line_items.where(:product_id => product_id).first
     if current_item 
        current_item.quantity = current_item.quantity.to_i + 1
      else
        current_item = LineItem.new(:product_id=>product_id)
        line_items << current_item
      end
        current_item
   end

  def total_price
   Array.wrap(line_items).sum { |item| item.total_price }
  end

我的购物车/ show.html.rb

<div class="cart_title" >Your Cart</div>
    <table>
        <% for item in @cart.line_items %>
    <tr>
        <td><%= item.quantity %>&times;</td>
        <td><%= item.product.title %></td>
    <td class="item_price" ><%= number_to_currency(item.total_price) %></td>
    </tr>
    <% end %>
        <tr class="total_line" >
        <td colspan="2" >Total</td>
        <td class="total_cell" ><%= number_to_currency(@cart.total_price) %></td>
    </tr>
    </table>
        <%= button_to 'Empty cart', @cart, :method => :delete,
        :confirm => 'Are you sure?' %>

我的模型/ line_item.rb

def total_price
     if product.price 
       product.price = product.price * quantity
     else
        0.to_d
     end
   end

完整追踪

app/models/line_item.rb:14:in `*'
app/models/line_item.rb:14:in `total_price'
app/views/carts/show.html.erb:7:in `block in _app_views_carts_show_html_erb___389431939__618846538'
activerecord (3.1.1) lib/active_record/associations/collection_proxy.rb:91:in `each'
activerecord (3.1.1) lib/active_record/associations/collection_proxy.rb:91:in `method_missing'
app/views/carts/show.html.erb:3:in `_app_views_carts_show_html_erb___389431939__618846538'
actionpack (3.1.1) lib/action_view/template.rb:144:in `block in render'
activesupport (3.1.1) lib/active_support/notifications.rb:55:in `instrument'
actionpack (3.1.1) lib/action_view/template.rb:142:in `render'
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:40:in `block (2 levels) in render_template'
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument'
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:33:in `instrument'
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:39:in `block in render_template'
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:47:in `render_with_layout'
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:38:in `render_template'
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:12:in `block in render'
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats'
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:9:in `render'
actionpack (3.1.1) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.1.1) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.1.1) lib/abstract_controller/rendering.rb:120:in `_render_template'
actionpack (3.1.1) lib/action_controller/metal/streaming.rb:250:in `_render_template'
actionpack (3.1.1) lib/abstract_controller/rendering.rb:114:in `render_to_body'
actionpack (3.1.1) lib/action_controller/metal/renderers.rb:30:in `render_to_body'
actionpack (3.1.1) lib/action_controller/metal/compatibility.rb:43:in `render_to_body'
actionpack (3.1.1) lib/abstract_controller/rendering.rb:99:in `render'
actionpack (3.1.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.1.1) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/led/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
activesupport (3.1.1) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.1.1) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.1.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.1.1) lib/action_controller/metal/mime_responds.rb:268:in `block in retrieve_response_from_mimes'
actionpack (3.1.1) lib/action_controller/metal/mime_responds.rb:195:in `call'
actionpack (3.1.1) lib/action_controller/metal/mime_responds.rb:195:in `respond_to'
app/controllers/carts_controller.rb:22:in `show'
actionpack (3.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.1.1) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.1.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.1.1) lib/active_support/callbacks.rb:416:in `_run__371692982__process_action__266900328__callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.1) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.1) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.1) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.1) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:532:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.6) lib/rack/etag.rb:23:in `call'
rack (1.3.6) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/flash.rb:243:in `call'
rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/cookies.rb:331:in `call'
activerecord (3.1.1) lib/active_record/query_cache.rb:62:in `call'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.6) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.6) lib/rack/lock.rb:15:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.1) lib/rails/engine.rb:456:in `call'
railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
/home/led/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/led/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/led/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

我在将我的第一个项目添加到我的购物车时出现问题但它失败了但是当第二次添加时它工作正常我只是想知道我的添加产品方法是否正确? 提前感谢我们的力量!

**编辑添加的信息**

跟踪我发现的信息我发现在我第一次尝试将项目添加到购物车时,我的第一次尝试的数量不是写入数据库,1默认(例如是id 128),即使我添加了一个默认的quaunty =&GT;我的迁移文件中有1个。

我的migragein文件 20120218201140_add_quantity_to_line_item.rb

def self.up
    add_column :line_items, :quantity, :integer, :default => 1
end

已经rake db:迁移它仍然默认选项不起作用?

我的数据库

id          product_id  cart_id     created_at                  updated_at                  quantity    price     
----------  ----------  ----------  --------------------------  --------------------------  ----------  ----------
127         18          37          2012-02-23 09:05:40.594604  2012-02-23 09:05:53.560155  1           321.11    
128         15          37          2012-02-23 09:07:53.013923  2012-02-23 09:07:53.013923              42.95  

1 个答案:

答案 0 :(得分:0)

我解决了我的问题 回滚迁移20120218201140_add_quantity_to_line_item.rb 并再次迁移它,它解决了我的问题感谢您的帮助 对我们更有力量