NoMethodError when attempting to display index

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 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!

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

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

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

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)

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? :smiley: 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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs