# Strange nil object message

**URL:** <https://www.ruby-forum.com/t/strange-nil-object-message/48889>\
**Category:** Rails\
**Created:** [November 26, 2005, 7:26am UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889 "2005-11-26T07:26:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 7:26am UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/1 "2005-11-26T07:26:52Z")

</div>

Hi,

I’m on winXP, learning to use RoR to talk with MySQL.

I’m tweaking the .rhtml code in views to change the default look of the  
“edit” and “new” pages. I changed the edit.rhtml page so that I get a  
list of records in addition to the record editing form. I merely  
copy-pasted the code from the list.rhtml page for this.

I get a “nil object” error. If I remove the code that actually lists the  
entries and only have the code that writes the column headings  
everything goes fine.

Is this a ruby bug?

thanks!

---

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 7:38am UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/2 "2005-11-26T07:38:32Z")

</div>

I haven’t used scaffolding much but here is my guess.

In the list action it probably has something like

def list  
@items = Items.find(:all)  
end

Copy and paste this line to your edit action so that the items are  
available  
in the view.

-Peter

---

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 4:15pm UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/3 "2005-11-26T16:15:57Z")

</div>

Hi Peter,

Thanks for your reply. I don’t know if I need that. Here is the code

#copied verbatim from list.rhtml

 \<% for column in Note.content\_columns %\> \<% end %\> #This part works just fine

\<% for note in @notes %\>

 \<% for column in Note.content\_columns %\> \<% end %\> \<% end %\> #This part screws up

| \<%= column.human\_name %\> |
| --- |
| \<%=h note.send(column.name) %\> |

#left unchanged for edit.rhtml

# Editing note \<%=@note.title%\>

\<%= start\_form\_tag :action =\> ‘update’, :id =\> @note %\>  
\<%= render :partial =\> ‘form’ %\>  
\<%= submit\_tag ‘Edit’ %\>  
\<%= end\_form\_tag %\>

\<%= link\_to ‘Show’, :action =\> ‘show’, :id =\> @note %\> |  
\<%= link\_to ‘Back’, :action =\> ‘list’ %\>

So there is indeed an iterator over notes. Its just odd that I get an  
error when this runs from the edit view, but not from the list view.  
-kaushik

---

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 8:41pm UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/4 "2005-11-26T20:41:12Z")

</div>

Oh you be good Peter!

Yes, that worked. Thanks for your prompt replies. Adding

\<% @notes = Note.find(:all) %\>

worked.

I’m still not sure why, though, since this code is not there in  
list.rhtml and list still works ☹

-kaushik

---

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 8:49pm UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/5 "2005-11-26T20:49:23Z")

</div>

You have controllers and views. Your views shouldn’t contain code such  
as  
\<% @notes = Note.find(:all) %\>

This code isn’t in the list.rhtml, but it is in the  
app/controllers/yourcontroller.rb. Open that controller and look at

def list  
@notes = Note.find(:all)  
end

This code is executed before the view, so the @notes is in the  
list.rhtml view. But in the

def edit  
…  
end

There isn’t @notes = Note.find(:all).

So if you alter it like this:  
def edit  
…

@notes = Note.find(:all)  
end

It will work. You could also do it like this:

def edit  
…

list  
end

So you call the list action which sets the @notes variable in the edit  
action.

---

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 7:07pm UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/6 "2005-11-26T19:07:24Z")

</div>

I don’t think it is so strange. Action variables are made available if  
then  
view. If your edit action does not have @notes then neither will your  
view.

Add “@notes = Note.find(:all)” to you edit action.

Peter

---

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 9:24pm UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/7 "2005-11-26T21:24:44Z")

</div>

On 11/26/05, Kaushik G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Oh you be good Peter!
> 
> Yes, that worked. Thanks for your prompt replies. Adding
> 
> \<% @notes = Note.find(:all) %\>

I think the view is not the best place to put this. You want this in  
your  
edit action (not view). Even if you do want it in your view try this in  
your  
controller file.

def edit

# your usual edit action code

# add this line

@notes = Note.find(:all)

end

I’m still not sure why, though, since this code is not there in

> list.rhtml and list still works ☹

because “@notes = Note.find(:all)” is in the list action (not view).  
Look  
for it. Find it. Scaffold put it there didn’t it? It doesn’t matter if  
you  
write the code or scaffold writes the code for you. In either case  
@notes  
has to be filled. Rails doesn’t fill it for you magically. Somewhere  
your  
code does.

When you had the nil error it was because Rails had never seen @notes  
before  
and yet you still try to iterate over it. You have to tell Rails to fill  
@notes in the edit action if you want to use @notes in the edit view.

Instance variable in an action are available in the associated view.

I hope this makes more sense.

Peter

---

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 11:12pm UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/8 "2005-11-26T23:12:18Z")

</div>

Hey Jules and Peter,

Thanks for your explanations. RoR is making a bit more sense now. I  
never thought to look in the controllers code. Is there a manual or  
something that would take me through RoR systematically? The stuff I’ve  
found so far uses exmples, but as I’m finding out, I’m not understanding  
the structure of RoR very well.

My skill levels are that I’m fairly experienced in C,C++ and in html and  
a bit of css.

thanks  
-kaushik

---

<div class="post-metadata">

**Author:** ![kghose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kghose](https://www.ruby-forum.com/u/kghose)\
**Post date:** [November 26, 2005, 11:45pm UTC](https://www.ruby-forum.com/t/strange-nil-object-message/48889/9 "2005-11-26T23:45:16Z")

</div>

kghose wrote:

> My skill levels are that I’m fairly experienced in C,C++ and in html and  
> a bit of css.

Check out [www.softiesonrails.com](http://www.softiesonrails.com), it’s for those of us Microsofties  
trying to get our heads around concepts in rails.
