I''m following the Depot tutorial in the Rails book, and have just hit a wall. As instructed by the tutorial, I added the following code to the Product model: protected def validate errors.add(:price, "should be positive") unless price > 0.0 end When I attempt to add a new product, I''m getting an error: undefined method `>'' for nil:NilClass /app/models/product.rb:11:in `validate'' app/controllers/admin_controller.rb:21:in `create'' ./script/server:48 (This is on page 60 of the book). The only thing I did differently from the book is use Sqlite3 instead of MySQL. Everything was working until that ''errors.add'' line. I know there''s a place for bugs about the book (if this even is one) but I figured lots of people on this mailing list will be following the book, so it''d be good to have an answer here too. Thanks, Ben
Solution: errors.add(:price, "should be positive") unless price.to_f > 0.0 I guess because Sqlite doesn''t enforce data types everything comes across as a string. Ben On 5/27/05, Ben Myles <ben.myles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m following the Depot tutorial in the Rails book, and have just hit a wall. > > As instructed by the tutorial, I added the following code to the Product model: > > protected > def validate > errors.add(:price, "should be positive") unless price > 0.0 > end > > When I attempt to add a new product, I''m getting an error: > > undefined method `>'' for nil:NilClass > /app/models/product.rb:11:in `validate'' > app/controllers/admin_controller.rb:21:in `create'' > ./script/server:48 > > (This is on page 60 of the book). > > The only thing I did differently from the book is use Sqlite3 instead > of MySQL. Everything was working until that ''errors.add'' line. > > I know there''s a place for bugs about the book (if this even is one) > but I figured lots of people on this mailing list will be following > the book, so it''d be good to have an answer here too. > > Thanks, > Ben >
That''s odd; your original error was complaining about price being nil, no? On 5/27/05, Ben Myles <ben.myles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Solution: > > errors.add(:price, "should be positive") unless price.to_f > 0.0 > > I guess because Sqlite doesn''t enforce data types everything comes > across as a string. > > Ben > > On 5/27/05, Ben Myles <ben.myles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''m following the Depot tutorial in the Rails book, and have just hit a wall. > > > > As instructed by the tutorial, I added the following code to the Product model: > > > > protected > > def validate > > errors.add(:price, "should be positive") unless price > 0.0 > > end > > > > When I attempt to add a new product, I''m getting an error: > > > > undefined method `>'' for nil:NilClass > > /app/models/product.rb:11:in `validate'' > > app/controllers/admin_controller.rb:21:in `create'' > > ./script/server:48 > > > > (This is on page 60 of the book). > > > > The only thing I did differently from the book is use Sqlite3 instead > > of MySQL. Everything was working until that ''errors.add'' line. > > > > I know there''s a place for bugs about the book (if this even is one) > > but I figured lots of people on this mailing list will be following > > the book, so it''d be good to have an answer here too. > > > > Thanks, > > Ben > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Apparently Analagous Threads
- scaffold :product missing in Rails 2.0.2 version of depot tutorial?
- Could not retrieve catalog from remote server: undefined method `closed?' for nil:NilClass
- undefined method `key?' for nil:NilClass,bcrypt-ruby,has_secure_password
- undefined method `[]' for nil:NilClass
- ActionView::Template::Error (undefined method `strftime' for nil:NilClass)