Problem - getting a nil object in view?


#1

Hello All,

I am new to ruby programming,
Problem is -

In my controller -

I defined a private method like
:private
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
end

end

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

<%end%>
<%end%>

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?

Thanks,
Vikas


#2

On Feb 20, 3:52 am, Vikas G. removed_email_address@domain.invalid
wrote:

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.

Fred