1
我正在使用當前正在使用的系統,並且出現了不常使用的功能的錯誤。未定義的方法'各爲零:NilClass
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索引作用,這可能是問題。 – Wukerplank
在你的'OfferReceiptsController'中,你能顯示'index'動作嗎?看起來'@ offer_receipts'實例變量是nil。也許你沒有設置它? – Zajn