yatta20
yatta20

Reputation: 1

NoMethodError in Admin#index

I'm a newbie having an issue with Agile Web Development with Rails 2nd. Ruby verison 1.8.6. The issue started when the book instructed me to put scaffold product in the admin_controller.rb. I removed the line and now i'm getting the following error message.

NoMethodError in Admin#index Showing admin/index.html.erb where line #10 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 #10):

7:     <th>Image url</th>
8:   </tr>
9: 
10: <% for product in @product %>
11:   <tr>
12:     <td><%=h product.title %></td>
13:     <td><%=h product.description %></td>

RAILS_ROOT: C:/InstantRails-2.0-win/rails_apps/depot

Here's the controller information:admin_controller class AdminController < ApplicationController end

Here's the view information: Views\admin\index.html.erb

Listing products

<table>
  <tr>
    <th>Title</th>
    <th>Description</th>
    <th>Image url</th>
  </tr>

<% for product in @product %>
  <tr>
     <td><%=h product.title %></td>
    <td><%=h product.description %></td>
    <td><%=h product.image_url %></td>
    <td><%= link_to 'Show', product %></td>
    <td><%= link_to 'Edit', edit_product_path(product) %></td>
    <td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method          => :delete %></td>
      </tr>
<% end %>
</table>

<br />

<%= link_to 'New product', new_product_path %>

Here's the models information: Models\product.rb class Product < ActiveRecord::Base end

Any advise?

Upvotes: 0

Views: 5016

Answers (1)

miked
miked

Reputation: 4499

Sounds like @products isn't being set in your controller and you also have a possible typo in the view:

in views\admin\index.html.erb, change:

 <% for product in @product %>  

to:

 <% for product in @products %>

and ensure your controller has:

admin_controller.rb

class AdminController < ApplicationController 

  def index
    @products = Product.all
  end

end

Upvotes: 3

Related Questions