Forum: Ruby on Rails Nil object when an array was expected.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E49352a077a28a9c0b10831b4750f142?d=identicon&s=25 chhenning (Guest)
on 2007-01-23 18:43
(Received via mailing list)
Hi there, it's kind of a weird problem. I have a Person model and
Person controller. In the new method in my controller I try to retrieve
a collection of nationalities. The nationalities are stored in a
different table. So retrieving those should be like:

def new
    @person = =
    @person.birthday =
    @person.nationality =
    nationalities = Nationality.find( :all )

Unfortunately when Rails try to access the array in the new.rhtml it's
nil. Here, I create a selection list, as follows:

    <% form_for :gender do |form| %>
      <%= :gender, @genders )  %>
    <% end %>

The weird thing happens when I retrieve the array inside new.rhtml.
It's working here.

    <% form_for :nationality do |form| %>
        @nationalities = Nationality.find( :all ) :nationality, @nationalities )
    <% end %>

As stated in the new Web Development book those code should normally
reside in the controller.

Am I missing something?

Thanks ahead,
6372e690497a80453a1d3d120d8b8818?d=identicon&s=25 Jason Stewart (Guest)
on 2007-01-23 18:57
(Received via mailing list)
Try using an instance variable (@nationalities) in the controller
instead of a local variable (nationalities). Instance variables are
available in the views, but local variables created in the controller
method are not.


On Tue, 23 Jan 2007 17:29:04 -0000
"chhenning" <> wrote:

>     @person.nationality =
>   </p>
>       %>
> >

Jason Stewart              | Tel: 616-532-2300
Systems Administrator/     | Fax: 616-532-3461
Programmer                 | Email:
Right to Life of Michigan  | Web:
E49352a077a28a9c0b10831b4750f142?d=identicon&s=25 chhenning (Guest)
on 2007-01-23 20:04
(Received via mailing list)
Thanks Jason, I'm coming from C++ and though I'm probably more used to
C++ way of compiler errors. If an variables is not accessible in the
current scope why not just state that. But I guess Ruby is on a lot of
ways different to C++.

Anyway thanks, that was the solution.

This topic is locked and can not be replied to.