[英]undefined method `+' for nil:NilClass (NoMethodError) - Ruby
When the rails application is run, I get the following error which is shown clearly in an image, which is as below; 运行rails应用程序时,出现以下错误,该错误在以下图像中清晰显示:
index.html.erb 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 _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>
Any suggestions are most welcome. 任何建议都是最欢迎的。
Thank you in advance. 先感谢您。
You're using a variable that is local to the index view in the partial, which has no access to it. 您正在使用局部变量中索引视图本地的变量,该变量无法访问它。 Either pass it through as a local to the partial:
要么将其作为局部传递给局部:
<%= render partial: "xvaziri", collection: @xvaziris, balance: balance %>
or make it available to the whole request 或将其提供给整个请求
<% @balance = 0 %>
I would recommend the latter since you will be using it in many places. 我建议使用后者,因为您将在许多地方使用它。 Update all uses of the variable to
@balance
and you'll be set. 将变量的所有用法更新为
@balance
即可设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.