# Uninitialized constant Product

**URL:** <https://www.ruby-forum.com/t/uninitialized-constant-product/78237>\
**Category:** Rails\
**Created:** [December 4, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-product/78237 "2006-12-04T12:20:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Coady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tom\_Coady](https://www.ruby-forum.com/u/Tom_Coady)\
**Post date:** [December 4, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-product/78237/1 "2006-12-04T12:20:56Z")

</div>

I am actually following the instructions/code snippets from “Agile Web  
Development with Rails” and got as far as page 73 where I did ruby  
script/generate controller Store index, then edited

store\_controller.rb to  
class StoreController \< ApplicationController  
def index  
@products = Product.salable\_items  
end  
end

and product.rb to:  
def self.salable\_items  
find(:all,  
:conditions =\> “date\_available \<= now()”,  
:order =\> “date\_available desc”)  
end

but it keeps showing this error in [http://localhost:3000/store/](http://localhost:3000/store/) :  
NameError in StoreController#index

uninitialized constant Product

RAILS\_ROOT: script/…/config/…  
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:100:in  
`const_missing' /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const\_missing’  
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:133:in  
`send' /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:in `const\_missing’  
app/controllers/store\_controller.rb:3:in `index’

FWIW index.rhtml reads:

\<% for product in @products %\> 

```
<td>
  <img src="<%= product.image_url %>"/>
</td>

<td width="450">
  <h3><%=h product.title %></h3>
  <small>
     <%= product.description %>
  </small>
  <br/>
  <strong>$<%= sprintf("%0.2f", product.price) %></strong>
  <%= link_to 'Add to Cart',
              :action => 'add_to_cart',
              :id => product %>
  <br/>
</td>

```

\<% end %\>

|
| |
| 
* * *
 |

---

<div class="post-metadata">

**Author:** ![Tom\_Coady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tom\_Coady](https://www.ruby-forum.com/u/Tom_Coady)\
**Post date:** [December 4, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-product/78237/2 "2006-12-04T13:19:04Z")

</div>

Solved! Use the foloowing product.rb instead:

class Product \< ActiveRecord::Base

validates\_presence\_of :title  
validates\_presence\_of :description  
validates\_presence\_of :image\_url  
validates\_uniqueness\_of :title  
validates\_numericality\_of :price  
validates\_format\_of :image\_url,  
:with =\> %r{http:.+.(gif|jpg|png)$}i,  
:message =\> “must be a URL for a GIF, JPG,  
or PNG image”

# Return a list of products we can sell (which means they have to be

# available). Show the most recently available first.

def self.salable\_items  
find(:all,  
:conditions =\> “date\_available \<= now()”,  
:order =\> “date\_available desc”)  
end

protected

# Validate that the product price is a positive Float.

def validate #:doc:  
errors.add(:price, “should be positive”) unless price.nil? || price

> = 0.01  
> end

end

---

<div class="post-metadata">

**Author:** ![Tom\_Coady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tom\_Coady](https://www.ruby-forum.com/u/Tom_Coady)\
**Post date:** [December 10, 2006, 1:16am UTC](https://www.ruby-forum.com/t/uninitialized-constant-product/78237/3 "2006-12-10T01:16:24Z")

</div>

drat! I still get the error when using the so-called “solved” code. Is  
there good code elsewhere?
