繁体   English   中英

erb数组迭代中nil:NilClass的未定义方法`each'

[英]undefined method `each' for nil:NilClass on an erb array iteration

我目前在Rails 5应用程序中工作,您可以在其中搜索名字或姓氏,然后将显示该帐户的客户记录。 但是我从搜索算法得到一个Nil对象返回。

customers_controller:

class CustomersController < ApplicationController
  def index
    if params[:keywords].present?
      @keywords = params[:keywords]
      customer_search_term = CustomerSearchTerm.new(@keywords)
      @customer = Customer.where(
        customer_search_term.where_clause,
        customer_search_term.where_args).
        order(customer_search_term.order)
    else
      @customers = []
    end
  end
end

如您所见,如果没有找到记录,则假定返回一个空数组,但返回一个Nil对象。

客户/ index.html.erb

[![<header>
  <h1 class="h2">Customer Search</h1>
</header>

<section class="search-form">
  <%= form_for :customers, method: :get do |f| %>
    <div class="input-group input-group-lg">
      <%= label_tag :keywords, nil, class: "sr-only" %>
      <%= text_field_tag :keywords, nil,
                          placeholder: "First Name, Last Name or Email Address",
                          class: "form-control input-lg" %>

      <span class="input-group-btn">
        <%= submit_tag "Find Customers", class: "btn btn-primary btn-lg" %>
      </span>
    </div>
  <% end %>
</section>

<section class="search-results">
  <header>
    <h1 class="h3">Results</h1>
  </header>
  <table class="table table-striped">
    <thead>
      <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Joined</th>
      </tr>
    </thead>
    <tbody>
      <% @customers.each do |customer| %>
        <tr>
          <td><%= customer.first_name %></td>
          <td><%= customer.last_name %></td>
          <td><%= customer.email %></td>
          <td><%= l customer.created_at.to_date %></td>
        </tr>
      <% end %>
    </tbody>
  </table>
</section>][1]][1]

您应该了解的第一件事是,如果尚未设置实例变量,则它们将返回nil 如果您说@fake_var == nil ,那么如果您从未在此之前定义过@fake_var ,那将是正确的。 您可以将其与常规局部变量进行对比,如果在定义它们之前尝试使用它们,则会引发NoMethodError。 例如, puts(fake_var)将为fake_var引发NoMethodError。

现在看看您的模板。 不管它将遍历@customers 如果未设置@customers ,则会看到NoMethodError,因为您不能在nil上调用each

最后,查看您的控制器操作:

  def index
    if params[:keywords].present?
      @keywords = params[:keywords]
      customer_search_term = CustomerSearchTerm.new(@keywords)
      @customer = Customer.where(
        customer_search_term.where_clause,
        customer_search_term.where_args).
        order(customer_search_term.order)
    else
      @customers = []
    end
  end

特别是当params[:keywords].present?时的情况params[:keywords].present? 在这种情况下,您永远不会设置@customers ,因此当模板尝试访问它时它将为nil

我认为,如果您只是将@customer =替换为@customers = ,它将解决您的问题。

您可以使用#to_a强制将其返回数组,该数组将nil转换为空数组

def index
  return [] unless params[:keywords]
  @keywords = params[:keywords]
  customer_search_term = CustomerSearchTerm.new(@keywords)
  @customer = Customer.where(
    customer_search_term.where_clause,
    customer_search_term.where_args).
    order(customer_search_term.order
  ).to_a
end

https://apidock.com/ruby/Array/to_a

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM