![](/img/trans.png)
[英]Ruby - NoMethodError (undefined method for nil:NilClass):
[英]undefined method `+' for nil:NilClass (NoMethodError) - Ruby
運行rails應用程序時,出現以下錯誤,該錯誤在以下圖像中清晰顯示:
index.html.erb
<% balance = 0 %>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="table-responsive myTable">
<table class="table listing text-center">
<tr class="tr-head">
<td>Date</td>
<td>Description</td>
<td>Amount</td>
<td>Discount</td>
<td>Paid</td>
<td>Balance</td>
</tr>
<%= render partial: "xvaziri", collection: @xvaziris %>
</table>
</div>
</div>
</div>
_xvaziri.html.erb
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-1"><%= xvaziri.date.strftime('%d/%m/%Y') %></td>
<td class="col-3"><%= span_with_possibly_red_color xvaziri.description %></td>
<td class="col-1"><%= number_with_precision(xvaziri.amount, :delimiter => ",", :precision => 2) %></td>
<td class="col-1 neg"><%= number_with_precision(xvaziri.discount, :delimiter => ",", :precision => 2) %></td>
<td class="col-1 neg"><%= number_with_precision(xvaziri.paid, :delimiter => ",", :precision => 2) %></td>
<% balance += xvaziri.amount.to_f - xvaziri.discount.to_f - xvaziri.paid.to_f %>
<% color = balance >= 0 ? "pos" : "neg" %>
<td class="col-1 <%= color %>"><%= number_with_precision(balance.abs, :delimiter => ",", :precision => 2) %></td>
</tr>
任何建議都是最歡迎的。
先感謝您。
您正在使用局部變量中索引視圖本地的變量,該變量無法訪問它。 要么將其作為局部傳遞給局部:
<%= render partial: "xvaziri", collection: @xvaziris, balance: balance %>
或將其提供給整個請求
<% @balance = 0 %>
我建議使用后者,因為您將在許多地方使用它。 將變量的所有用法更新為@balance
即可設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.