Strange nil object message

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!

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

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 <%[email protected]%>

<%= 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

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 :frowning:

-kaushik

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.

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

On 11/26/05, Kaushik G. [email protected] 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 :frowning:

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

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

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, it’s for those of us Microsofties
trying to get our heads around concepts in rails.