Kavya
Kavya

Reputation: 3

Array displays values in rails console . But return undefined method `[]' for nil:NilClass in log

I have a helper method which returns array

def site
  return Website::SITE.collect!{ |arr| arr if arr[1] != 'site_builder' }
end

Website::SITE return array in console

I call this method in view.

- site.each do |menu|
    tr
      td= menu[0]

Here it gives ActionView::Template::Error (undefined method `[]' for nil:NilClass):

Upvotes: 0

Views: 150

Answers (2)

Emu
Emu

Reputation: 5905

Let's redefine the function like this:

def site
   Website::SITE.compact.select{ |arr| arr if arr[1] != 'site_builder' }
end

It's because, the array Website::SITE contains nil value.

[["About ", "about"], ["Calendar", "calendar"], ["Gallery", "gallery"], ["Information", "information"], ["Manage ", "manage"], nil, ["Template", "website"]]

Upvotes: 0

Spinshot
Spinshot

Reputation: 315

If you didn't put the method in the helpers folder but directly to the controller, then you have to add a line of code after your method to make it work.

def site
  return Website::SITE.collect!{ |arr| arr if arr[1] != 'site_builder' }
end
helper_method :site

This should fix the error

Upvotes: 0

Related Questions