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/ :
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:
<table cellpadding="5" cellspacing="0">
<% for product in @products %>
<tr valign="top">
<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>
</tr>
<tr><td colspan="2"><hr/></td></tr>
<% end %>
</table>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
drat! I still get the error when using the so-called "solved" code. Is there good code elsewhere? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Coady wrote:> 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/ : > 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: > <table cellpadding="5" cellspacing="0"> > <% for product in @products %> > <tr valign="top"> > > <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> > </tr> > > <tr><td colspan="2"><hr/></td></tr> > > <% end %> > </table>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---