yellowreign
yellowreign

Reputation: 3638

Nil value for array in Rails 3

I feel like a moron asking this, but since I'm a new to developing, and I've been sitting here for an hour making something so simple work, I need to ask.

I want to show each Widget, but I keeping getting:

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

I tried using <%= debug @widgets %> and <%= @widgets.inspect %>, but nothing shows in my command prompt or browser.

I was trying to copy the each do statement on http://guides.rubyonrails.org/layouts_and_rendering.html

In my controller, called Pages Controller, I have:

def widgets_to_show
  @widgets = Widget.all
end 

In my view (pages#widgets_to_show), I have:

<% @widgets.each do |widget| %>
  <%= widget.title %>
<% end %>

I'm trying to display it on a page that uses the High Voltage gem (https://github.com/thoughtbot/high_voltage) if that makes a difference.

Upvotes: 0

Views: 215

Answers (1)

Dty
Dty

Reputation: 12273

As @DaveNewton suggested try accessing the widget from your console (not irb). Do the following from your terminal

# from your rails project dir
$ rails c
$ w = Widgets.all
$ puts w
$ w.each { |n| puts n.title }

If all of that works then we need more info to figure out what the problem is.

Edit:

My guess is that it has something to do with using high voltage on the same page. It's a gem for creating static pages so maybe that's interfering with showing your widgets - since what you're doing is something dynamic.

Upvotes: 1

Related Questions