I am working on a system that is currently in use and an error for a feature that is not very often used has come up.
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>
This error happens when this partial render page is currently being displayed:
<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>
Once the one time offer is clicked on, this page is loaded and this is the page the error happens on:
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>
and here are two other files that define the methods and scope of the One Time Offer receipt
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
There are other index.html.erb files for listing other things with the exact same for each loop, and they are working fine. I also checked the database and there are over 2000 rows so that can't be the issue.
Your instance variable, @offer_receipt
is not set in your #index
controller action.
You should add the #index
action. Something like this should work:
class OfferReceiptsController < ApplicationController
def index
@offer_receipts = OfferReceipt.all
end
end
You can also add additional logic in the #index
action. You might want to scope the instance variable to a current_user
, for example.
@offer_receipts = OfferReceipt.where(:user_id => current_user.id)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.