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