0

以前は機能していたメソッド「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 を追加して、違いが生じるかどうかを確認しようとしましたが、違いはありませんでした。

コンソールでまだ機能する場合、それはおそらくメソッド自体が壊れているのではないということですか?

どんな助けでも大歓迎です!ありがとう

4

1 に答える 1

0

prほとんどの場合、ゼロです。上記のコードでは、金額が 500 を超えるアイテムはprnil になります。

于 2013-11-04T20:11:54.980 に答える