[英]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.