Forum: Ruby on Rails But, my local variable 'product' is defined!!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ryan O. (Guest)
on 2008-12-21 19:08
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 -

<h1>The store catalog</h1>
<% for products in @products -%>
<div class="entry">
  <%= image_tag(product.image_url) %>
    <h3><%= h(product.title) %>
    <span class="price"><%= product.price %></span>
</div>
<% 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.
Hassan S. (Guest)
on 2008-12-21 19:17
(Received via mailing list)
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 :-) but...

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

> <% for products in @products -%>
> <div class="entry">
>  <%= 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
This topic is locked and can not be replied to.