Forum: Ruby on Rails Problem - getting a nil object in view?

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.
Vikas G. (Guest)
on 2009-02-20 05:52
Hello All,

I am new to ruby programming,
Problem is -

In my controller -

I defined a private method like
def _list(searchBy, moreResults = false)

# another code goes here.................
@items_by_language = {}
      @items.each do |item|
        @items_by_language[item.language]=[] if
@items_by_language[item.language] == nil
        @items_by_language[item.language] << item


# This @items_by_language is a hash which I am accessing in my view
named list.html.erb  But I get following error
 NoMethodError in Home#list

Showing home/list.html.erb where line #6 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 #6):

list.html.erb code -----------------------

<%@items_by_language.each do |key,val|%>
<%val.each do |items|%>

//////code performed on @items_by_language hash variable

In controller "_list method" is  private and I want it private . Thats
why it is not allowed me to access @items_by_language.

What should I do?

Frederick C. (Guest)
on 2009-02-20 11:48
(Received via mailing list)
On Feb 20, 3:52 am, Vikas G. <removed_email_address@domain.invalid>

> In controller "_list method" is  private and I want it private . Thats
> why it is not allowed me to access @items_by_language.

method visibility has nothing to do with instance variables. If you
are going to localhost:3000/some_controller/list then your _list
method is probably not even being called.

This topic is locked and can not be replied to.