简体   繁体   English

通过书在栏杆上的购物车

[英]Shopping cart in rails via book

Via such book: 通过这样的书:

Agile Web Development with Rails 使用Rails进行敏捷Web开发

i'm creating shopping cart for my application. 我正在为我的应用程序创建购物车。 There is such code: 有这样的代码:

class Cart < ActiveRecord::Base
    attr_accessible :id
    has_many :line_items, dependent: :destroy

  def add_article(article_id)
    current_item = line_items.find_by_ART_ID(article_id)
      if current_item
      current_item.quantity += 1
      else
      current_item = line_items.build(ART_ID: article_id)
      end
    current_item
  end
  def total_price
    line_items.to_a.sum { |item| item.total_price(item.ART_ID) }
  end
  def total_count
    line_items.to_a.sum { |item| item.quantity }
  end

end

On my previous project on rails 3.0.9 all was ok, but now it's say that 在我上一个关于Rails 3.0.9的项目中,一切都还可以,但是现在说

nil can't be coerced into Fixnum in db, quantity is null nil不能强制进入db中的Fixnum,数量为null

if i change my code for like this 如果我这样更改代码

current_item = line_items.find_by_ART_ID(article_id)
          if current_item
          current_item.quantity = 1
          else
          current_item = line_items.build(ART_ID: article_id)
          current_item.quntity = 1
          end
        current_item

all is good, but what's wrong? 一切都很好,但是怎么了? why rails 3.2.6 and ruby 1.9.3 don't understand my += assignments? 为什么Rails 3.2.6和ruby 1.9.3无法理解我的+ =分配?

Its not a problem with += , the problem is you are try to do this with your code. + =没问题,问题是您正在尝试使用代码来做到这一点。

nil + 1

quantity is nil for the first time. 数量首次为零。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM