# But, my local variable 'product' is defined!

**URL:** <https://www.ruby-forum.com/t/but-my-local-variable-product-is-defined/156148>\
**Category:** Rails\
**Created:** [December 21, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/but-my-local-variable-product-is-defined/156148 "2008-12-21T18:08:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryanoverton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ea924/32.png) [@ryanoverton](https://www.ruby-forum.com/u/ryanoverton)\
**Post date:** [December 21, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/but-my-local-variable-product-is-defined/156148/1 "2008-12-21T18:08:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ryanoverton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ea924/32.png) [@ryanoverton](https://www.ruby-forum.com/u/ryanoverton)\
**Post date:** [December 21, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/but-my-local-variable-product-is-defined/156148/2 "2008-12-21T18:17:56Z")

</div>

On Sun, Dec 21, 2008 at 9:08 AM, Ryan O.  
[removed\_email\_address@domain.invalid](mailto: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 🙂 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
