【问题标题】:undefined method 'each for nil:NilClass未定义的方法'each for nil:NilClass
【发布时间】:2014-11-20 11:07:37
【问题描述】:

我正在开发一个当前正在使用的系统,并且出现了一个不经常使用的功能的错误。

NoMethodError in Offer_receipts#index

Showing /Users....../app/views/offer_receipts/index.html.erb where   line #8 raised:

undefined method `each' for nil:NilClass
Extracted source (around line #8):

5:     <th>Patron</th>
6:     <th>One Time Offer</th>
7:   </tr>
8:   <% for offer_receipt in @offer_receipts %>
9:     <tr>
10:       <td><p> popld</p></td>
11:       <td><%= offer_receipt.patron_id %></td>

当前正在显示此部分渲染页面时会发生此错误:

<h2>Offers</h2>
<div id="offers">
<% if @offers.count > 0 %>
 < % @offers.each do |o| %>  
    <%= show_one_time_offer(o, @patron) %>
  <% end %>

<% else %>
  <strong>There are no offers currently available</strong>


<% end %>
</div>

点击一次性优惠后,将加载此页面,这是发生错误的页面:

index.html.erb

<% title "Offer Receipts" %>

<table>
  <tr>
    <th>Patron</th>
    <th>One Time Offer</th>
  </tr>
  <% for offer_receipt in @offer_receipts %>
    <tr>
      <td><%= offer_receipt.patron_id %></td>
      <td><%= offer_receipt.one_time_offer_id %></td>
      <td><%= link_to "Show", offer_receipt %></td>
      <td><%= link_to "Edit", edit_offer_receipt_path(offer_receipt) %></td>
      <td><%= link_to "Destroy", offer_receipt, :confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% end %>
</table>

<p><%= link_to "New Offer Receipt", new_offer_receipt_path %></p>

这里还有另外两个文件,它们定义了一次性优惠收据的方法和范围

offer_receipt.rb

class OfferReceipt < ActiveRecord::Base
  belongs_to :patron
  belongs_to :one_time_offer
  validates :patron, :presence=>true
  validates :one_time_offer, :presence=>true
  require 'offer_receipt_validator.rb'
  validates_with OfferReceiptValidator


end

offer_receipts_controller.rb

class OfferReceiptsController < ApplicationController
  def create
    begin
      @patron = Patron.find(params[:patron])
    rescue ActiveRecord::RecordNotFound
      flash[:error] = "You must provide a patron for an offer receipt"
      redirect_to root_url
      return
    end
    @offer_receipt = OfferReceipt.new(:patron_id=>params[:patron], :one_time_offer_id=>params[:one_time_offer])
    if @offer_receipt.save && @patron
      redirect_to @patron, :notice => "Recorded offer receipt"
    else
      flash[:error] = "There was a problem saving your offer receipt"
      redirect_to @patron
    end
  end
end

还有其他 index.html.erb 文件用于列出每个循环完全相同的其他内容,并且它们工作正常。我还检查了数据库,有超过 2000 行,所以这不是问题。

【问题讨论】:

  • 我在您的 OfferReceiptsController 中没有看到索引操作,这可能是问题所在。
  • 在您的OfferReceiptsController 中,您能否显示index 操作?看起来@offer_receipts 实例变量为零。也许你没有设置它?

标签: ruby-on-rails


【解决方案1】:

您的实例变量 @offer_receipt 未在您的 #index 控制器操作中设置。

您应该添加#index 操作。像这样的东西应该可以工作:

class OfferReceiptsController < ApplicationController 
  def index
    @offer_receipts = OfferReceipt.all
  end
end

您还可以在#index 操作中添加其他逻辑。例如,您可能希望将实例变量的范围限定为 current_user

@offer_receipts = OfferReceipt.where(:user_id => current_user.id)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多