简体   繁体   English

未定义的方法“ +”,用于nil:NilClass(NoMethodError)-Ruby

[英]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应用程序时,出现以下错误,该错误在以下图像中清晰显示:

error.png

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.

相关问题 Ruby - NoMethodError(nil的未定义方法:NilClass): - Ruby - NoMethodError (undefined method for nil:NilClass): Ruby:NoMethodError:nil的未定义方法`&lt;&lt;&#39;:NilClass - Ruby: NoMethodError: undefined method `<<' for nil:NilClass undefined方法[]为nil:ruby中的NilClass(NoMethodError)...为什么? - undefined method [] for nil:NilClass (NoMethodError) in ruby… Why? Ruby on Rails NoMethodError:nil:NilClass的未定义方法“ []” - Ruby on Rails NoMethodError: undefined method `[]' for nil:NilClass nil:NilClass Ruby on Rails的未定义方法&#39;&gt;&#39;(NoMethodError) - undefined method `>' for nil:NilClass Ruby on Rails (NoMethodError) nil 的未定义方法“%”:NilClass (NoMethodError) Ruby on Rails - Undefined method `%' for nil:NilClass (NoMethodError) Ruby on Rails Ruby未定义方法`[]&#39;表示nil:NilClass(NoMethodError)错误 - Ruby undefined method `[]' for nil:NilClass (NoMethodError) error Ruby On Rails - NoMethodError:nil:NilClass 的未定义方法“[]” - Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass STORYBLOK,RUBY:nil:NilClass (NoMethodError) 的未定义方法“[]” - STORYBLOK, RUBY: undefined method `[]' for nil:NilClass (NoMethodError) NoMethodError:nil:NilClass的未定义方法“ []” - NoMethodError: undefined method `[]' for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM