But, my local variable 'product' is defined!


#1

Following a tutorial here and I had it pointed out to me that it may be
a slightly older version (Agile web development v2) so I’m hoping my
problem is just sytax that one of the gurus here can point out…

I have a view called store/index.rhtml; it looks like this -

The store catalog

<% for products in @products -%>
<%= image_tag(product.image_url) %>

<%= h(product.title) %> <%= product.price %>

<% end %>

It should just list some sample product data. But it tells me:

undefined local variable or method `product’ for
#<#Class:0x275be4c:0x275be24>

Yet, in my admin view a call regarding 'product works fine:
e.g. <%= product.image_url %>

My store controller looks like:

class StoreController < ApplicationController

def index
@products = Product.find_products_for_sale
end
end

Any advise? I’d be very thankful.


#2

On Sun, Dec 21, 2008 at 9:08 AM, Ryan O.
removed_email_address@domain.invalid wrote:

Following a tutorial here and I had it pointed out to me that it may be
a slightly older version (Agile web development v2) so I’m hoping my
problem is just sytax that one of the gurus here can point out…

When posting something like this, it’s always a good idea to
mention the version of Rails you’re using :slight_smile: but…

I have a view called store/index.rhtml; it looks like this -

<% for products in @products -%>

<%= image_tag(product.image_url) %>

It should just list some sample product data. But it tells me:

undefined local variable or method `product’ for
#<#Class:0x275be4c:0x275be24>

As it should – where do you see the variable ‘product’ defined in
the above code?

You can fix by changing to for product in @products which also
reads properly – one ‘product’ from a collection of ‘@products’.

HTH,

Hassan S. ------------------------ removed_email_address@domain.invalid