small medium large xlarge

Generic-user-small
11 Nov 2010, 06:27
Resty Cena (5 posts)

Hello, I’m on page 111 of version b10. I’m getting the following error. What could be wrong? Thanks.

NoMethodError in Store#index

Showing /Users/restycena/work/depot/app/views/store/index.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):

3: <% end %> 4: 5: <h1>Your Pragmatic Catalog</h1> 6: <% @products.each do |product| %> 7: <div class="entry" > 8: <%= image_tag(product.image_url) %> 9: <h3><%= product.title %></h3>

Generic-user-small
13 Nov 2010, 18:49
Resty Cena (5 posts)

Hi, Still stuck on this error. Any ideas? Thanks.

Micahsq_pragsmall
13 Nov 2010, 20:19
Micah Alcorn (4 posts)

Please re-post your code and precede it with “< p r e > < c o d e >” (without spaces) so that the forum does not try to execute it.

Generic-user-small
14 Nov 2010, 05:42
Resty Cena (5 posts)

This is the code, straight from the book:

<% if notice %>

<%= notice %>

<% end %>

Your Pragmatic Catalog

<% @products.each do |product| %> <div class="entry" > <%= image_tag(product.image_url) %> <h3><%= product.title %></h3> <%= sanitize product.description %> <div class="price_line" > <%= product.price %> </div> </div> <% end %>

This is the result. Thanks.

< p r e >< c o d e > 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):

3: <% end %> 4: 5: <h1>Your Pragmatic Catalog</h1> 6: <% @products.each do |product| %> 7: <div class="entry" > 8: <%= image_tag(product.image_url) %> 9: <h3><%= product.title %></h3>

Generic-user-small
14 Nov 2010, 05:46
Resty Cena (5 posts)

This is the code, straight from the book:

<% if notice %>

<%= notice %>

<% end %>

Your Pragmatic Catalog

<% @products.each do |product| %>
<%= image_tag(product.image_url) %>

<%= product.title %>

<%= sanitize product.description %>
<%= product.price %>
<% end %> This is the result. Thanks. < p r e >< c o d e > 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): 3: <% end %> 4: 5:

Your Pragmatic Catalog

6: <% @products.each do |product| %> 7:
8: <%= image_tag(product.image_url) %> 9:

<%= product.title %>

Mypicweb_pragsmall
14 Nov 2010, 19:54
Steve Mills (4 posts)

I wish I could help you with this topic, but unfortantely I’m a newbie to Rails too.

However I can offer some encouragement that the code does work - I got past that point and it works.

I find that Ruby is well documented but I find the documentation for Rails quite obscure; in particular it’s not easy to see how all of the pieces fit together.

For example: if I’ve got it right then the Fixtures during Test create the “products” object that is “missing”/nil above. However, logically that cannot be the fixtures when you’re simply running your application in the Development environment because the data seen is coming from the development database and not from the Fixtures. So maybe the clues to solving this lie in the data? Is your database up and running and complete with the test data?

I am still unable to run Test even though the app works perfectly because it seems that rails loads the Fixtures and then immediately drops the table so the Test fails because the table is not found.

Generic-user-small
15 Nov 2010, 00:19
Resty Cena (5 posts)

Steve, Yeah, I’m sure a lot of people have gotten past that point, or else there’d be more error reports. Seems like it can’t find the product record?

Yes, how all the pieces fit together is what I consider the major learning stumbling block. You do something in one place and things happen in other places – other files – you don’t know about. I’m longing for an editor where I can see the relationships of the different pieces visually as I code as it happens, in one multi-part collapsible file maybe, or however it can be done.

But this is my second try at learning Rails. Apart from the above discomfort, and the fact that the syntax favors writers but not readers (i.e., experts but not beginners), there are many good things about it that I am determined to learn it this time.

Also the Rails 4th book assumes imjportant information that intermediate readers may know but not beginners. Perhaps it is trying to spare the Rails 3rd readers from dying of boredom.

Thanks for your encouragement.

Generic-user-small
16 Nov 2010, 04:11
Jimmy Dee (1 post)

I can give you a little hint here, though I haven’t actually done that exercise from the book, and I don’t know exactly what your problem is.

The index is an instance of a route, which is defined in config/routes.rb. You can see your routes by running rake routes. Often, as with the index route, the path name associated with the route (index) is the name of the controller method that is executed when you visit that path (that’s what the route is, an association between a path and a method). In this case, I’m quite sure the controller method that is building this view is StoreController#index, in app/controllers/store_controller.rb. That method has to set the @products instance variable in order for the view to be properly rendered. It’s not being set, or, as you suggest, the search is returning nil. That’s where I’d look.

1378470_10201594379893417_697049378_n_pragsmall
30 Nov 2010, 14:47
Chad Tipton (14 posts)

I am still fairly new to ruby and rails and it might be a function of HTML in the forums but it looks like an issue with the percent sign. There should be “less than” followed by a percent to start a ruby block and then a percent followed by “greater than”. The examples above are missing a lot of percent signs. And like I said it may be a function of the discussion forums.

Generic-user-small
27 Jun 2013, 23:32
Eugene Kim (1 post)

Hi I’m having this problem as well and I’m at a loss as to what I should be doing… help please!

You must be logged in to comment