代码之家  ›  专栏  ›  技术社区  ›  Steven Aguilar

在erb数组迭代上为nil:NilClass定义的未定义方法“each”

  •  0
  • Steven Aguilar  · 技术社区  · 7 年前

    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对象。

    [![<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]
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   max pleaner    7 年前

    您应该了解的第一件事是实例变量返回 nil @fake_var == nil 如果你从未定义 @fake_var puts(fake_var) 将为fake_var提出命名错误。

    现在看看你的模板。不管发生什么事情 @customers . 如果 @客户 尚未设置,您将看到一个NoMethodError,因为您无法调用 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? . 你从来没有设定 @客户 在这种情况下是这样的 当模板尝试访问它时。

    我认为如果你简单地更换 @customer = @customers = 这会解决你的问题。

        2
  •  0
  •   Exwire    7 年前

    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