簡體   English   中英

Rails為nil:NilClass使用的未定義方法`+'

[英]Rails undefined method `+' for nil:NilClass

我有一個以前一直在使用的方法“ calc_price”,現在仍然可以在控制台中使用,但是現在在瀏覽器中出現以下錯誤:

NoMethodError in Quotes#index

undefined method `+' for nil:NilClass

18:             Price: $<%= f.calc_price %><br />

app/models/quote.rb:33:in `block in calc_price'
app/models/quote.rb:13:in `calc_price'
app/views/quotes/index.html.erb:18:in `block in _app_views_quotes_index_html_erb__1788058106025144185_70227449765940'
app/views/quotes/index.html.erb:15:in `each' 
app/views/quotes/index.html.erb:15:in `_app_views_quotes_index_html_erb__1788058106025144185_70227449765940'

它仍然可以在控制台中工作的事實令我感到困惑,尤其是因為我根本沒有更改方法來使其中斷。 方法:

def calc_price
    self.items.each do |item|
        pr = if item.amount < 10
            item.product.pricerange0
        elsif item.amount < 25 
            item.product.pricerange1
        elsif item.amount < 50 
            item.product.pricerange2
        elsif item.amount < 100
            item.product.pricerange3
        elsif item.amount < 250
            item.product.pricerange4
        elsif item.amount < 500
            item.product.pricerange5
        end
        screens = 0
        sd = item.shirtdesigns.count
        pd = item.pantdesigns.count
        screens = (sd+pd)
        screenprice = (screens*25)
        inkprice = ((item.inkcolors-1)*0.5)
        newprice = ((pr+inkprice)*item.amount+screenprice)
        item.price = newprice
        item.save
    end
    newprice = self.items.sum('price')
    self.price = newprice
    self.save
    return self.price
end

報價控制器

def index
  @quote = Quote.find(:all)
  @myquotes = Quote.find(:all, :conditions => { :user_email => current_user.email })
end

我嘗試添加screenprice = 0,newprice = 0和inkprice = 0,以查看是否會有所不同,但事實並非如此。

如果它仍然可以在控制台中運行,這是否意味着不是方法本身被破壞了?

任何幫助將不勝感激! 謝謝

pr很可能是零。 數量大於500的項目將導致pr在上面的代碼中為nil。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2025 STACKOOM.COM