# NoMethodError when attempting to display index

**URL:** <https://www.ruby-forum.com/t/nomethoderror-when-attempting-to-display-index/137492>\
**Category:** Rails\
**Created:** [May 13, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-attempting-to-display-index/137492 "2008-05-13T17:35:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![noreaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@noreaster](https://www.ruby-forum.com/u/noreaster)\
**Post date:** [May 13, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-attempting-to-display-index/137492/1 "2008-05-13T17:35:17Z")

</div>

Ruby N. alert. This is a pretty basic question, probably.

I’m tinkering around with putting a Rails app on top of legacy database  
tables. Assume that the tables and their structures can not change. The  
database type is Oracle, but I don’t think that database connectivity is  
a problem, because I can view an individual object via Rails. Going to  
[http://127.0.0.1:3005/advisories/ZZ](http://127.0.0.1:3005/advisories/ZZ) yields the following:

Advisory: ZZ

Descr: MISCELLANEOUS ADVISORY

Sort order: 0

Exclusivity: 0

Edit | Back

Okay, great, so, that’s all well and good. However, when I click “Back”  
or get rid of the “/ZZ” on the URL, I get the following error:

NoMethodError in Advisories#index  
Showing advisories/index.html.erb where line #11 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

Extracted source (around line #11):

8: Exclusivity  
9:   
10:  
11: \<% for advisory in @advisories %\>  
12:   
13: \<%=h advisory.advisory %\>  
14: \<%=h advisory.descr %\>

(truncated)

Kind of seems like there must be some sort of problem in the  
controller’s index method, no? How come it has no problem getting one  
particular entry, but listing the entire table’s contents doesn’t work?

Here’s the table structure (notice that since this is a legacy table,  
there is no “id” column):

VIEW advisory  
Name Null? Type

* * *

* * *

ADVISORY VARCHAR2(4)  
DESCR VARCHAR2(80)  
SORT\_ORDER NUMBER(3)  
EXCLUSIVITY NUMBER(3)

Any ideas? Thanks very much in advance for your help!

---

<div class="post-metadata">

**Author:** ![noreaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@noreaster](https://www.ruby-forum.com/u/noreaster)\
**Post date:** [May 13, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-attempting-to-display-index/137492/2 "2008-05-13T17:38:21Z")

</div>

By the way, I should mention that the controller I have hasn’t been  
modified since I generated it with Ruby 2.0’s scaffold. Also, here’s my  
model:

class Advisory \< ActiveRecord::Base  
set\_primary\_key :advisory  
end

---

<div class="post-metadata">

**Author:** ![noreaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@noreaster](https://www.ruby-forum.com/u/noreaster)\
**Post date:** [May 13, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-attempting-to-display-index/137492/3 "2008-05-13T18:00:32Z")

</div>

On 13 May 2008, at 16:35, Gabe Heafitz wrote:

> Ruby N. alert(truncated)
> 
> Kind of seems like there must be some sort of problem in the  
> controller’s index method, no? How come it has no problem getting one  
> particular entry, but listing the entire table’s contents doesn’t  
> work?

by default those are different methods (index & show)  
If you suspect something is wrong with the index method, why not show  
us what it is?

Fred

---

<div class="post-metadata">

**Author:** ![noreaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@noreaster](https://www.ruby-forum.com/u/noreaster)\
**Post date:** [May 13, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-attempting-to-display-index/137492/4 "2008-05-13T18:07:08Z")

</div>

On 13 May 2008, at 17:04, Gabe H. wrote:

> > > particular entry, but listing the entire table’s contents doesn’t

Well the view is using @advisories but the controller is using  
@advisory which obviously isn’t going to work

Fred

---

<div class="post-metadata">

**Author:** ![noreaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@noreaster](https://www.ruby-forum.com/u/noreaster)\
**Post date:** [May 13, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-attempting-to-display-index/137492/5 "2008-05-13T18:04:43Z")

</div>

Frederick C. wrote:

> On 13 May 2008, at 16:35, Gabe Heafitz wrote:
> 
> > Ruby N. alert(truncated)
> > 
> > Kind of seems like there must be some sort of problem in the  
> > controller’s index method, no? How come it has no problem getting one  
> > particular entry, but listing the entire table’s contents doesn’t  
> > work?
> 
> by default those are different methods (index & show)  
> If you suspect something is wrong with the index method, why not show  
> us what it is?
> 
> Fred

Well, like I said, it’s just the basic controller as generated by the  
scaffold. So, that gives us:

class AdvisoriesController \< ApplicationController

# GET /advisory

# GET /advisory.xml

def index  
@advisory = Advisory.find(:all)

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @advisory }
end

```

end

# GET /advisory/1

# GET /advisory/1.xml

def show  
@advisory = Advisory.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @advisory }
end

```

end

(truncated)

---

<div class="post-metadata">

**Author:** ![noreaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@noreaster](https://www.ruby-forum.com/u/noreaster)\
**Post date:** [May 13, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-attempting-to-display-index/137492/6 "2008-05-13T18:11:23Z")

</div>

Frederick C. wrote:

> On 13 May 2008, at 17:04, Gabe H. wrote:
> 
> > > > particular entry, but listing the entire table’s contents doesn’t
> 
> Well the view is using @advisories but the controller is using  
> @advisory which obviously isn’t going to work
> 
> Fred

Ah ha. Well, there you go. I wasn’t lying when I said “Ruby N. alert”,  
now, was I? 😃 I guess I just figured that Ruby’s pluralizations and  
inflections would take care of that. I guess I’ll have to pay closer  
attention to that in the future.

Thanks very much!
