I''m using rails to learn ruby so sorry if this is a really dumb question. I"m getting this output but I don''t know where to look for the problem. Here is the error output from the log. ActionView::TemplateError (undefined method `to_f'' for #<Product:0xb7906f88>) on line #16 of app/views/store/display_cart.rhtml: 13: <tr> 14: <td><%= item.quantity %></td> 15: <td><%= h(product.title) %></td> 16: <td align="right"><%= item.unit_price %></td> 17: <td align="right"><%= item.unit_price * item.quantity %></td> 18: </tr> 19: <% end -%> /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:1501:in `method_missing'' (eval):1:in `unit_price'' I know that to_f tries to make a variable into a float but I don''t know what is trying to do this. -- Posted via http://www.ruby-forum.com/.
How is item.unit_price implemented? It could be that the error message is a bit misleading - the error may not be the .rhtml code itself but inside the unit_price method somewhere. Just a guess. -- Posted via http://www.ruby-forum.com/.
Here is my model: class LineItem < ActiveRecord::Base belongs_to :product def self.for_product(product) item = self.new item.quantity = 1 item.product = product item.unit_price = product.price item end end -- Posted via http://www.ruby-forum.com/.
On Dec 13, 2005, at 11:48 PM, charlie bowman wrote:> I''m using rails to learn ruby so sorry if this is a really dumb > question. I"m getting this output but I don''t know where to look for > the problem. Here is the error output from the log. > > ActionView::TemplateError (undefined method `to_f'' for > #<Product:0xb7906f88>) on line #16 of > app/views/store/display_cart.rhtml: > 13: <tr> > 14: <td><%= item.quantity %></td> > 15: <td><%= h(product.title) %></td> > 16: <td align="right"><%= item.unit_price %></td> > 17: <td align="right"><%= item.unit_price * item.quantity %></td> > 18: </tr> > 19: <% end -%> > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/ > active_record/base.rb:1501:in > `method_missing'' > (eval):1:in `unit_price'' > > > I know that to_f tries to make a variable into a float but I don''t > know > what is trying to do this.I''d guess it''s the multiplication that''s trying to coerce the object types, but that''s on line 17. In any case, I''d try to change your model: from... item.quantity = 1 to... item.quantity = 1.0 -- -- Tom Mornini
I made that change (quantity = 1.0) and it didn''t help. I also greped the whole folder for mentions of "unit" or "quantitiy". My only references to these are the Model I pasted in earlier and in the template. Where should I be looking for the problem. I''m too new at ruby to know how to find the error. ActionView::TemplateError (undefined method `to_f'' for #<Product:0xb7906f88>) on line #16 of app/views/store/display_cart.rhtml: 13: <tr> 14: <td><%= item.quantity %></td> 15: <td><%= h(product.title) %></td> 16: <td align="right"><%= item.unit_price %></td> 17: <td align="right"><%= item.unit_price * item.quantity %></td> 18: </tr> 19: <% end -%> /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:1501:in `method_missing'' (eval):1:in `unit_price'' I know that to_f tries to make a variable into a float but I don''t know what is trying to do this. Tom Mornini wrote:> On Dec 13, 2005, at 11:48 PM, charlie bowman wrote: > >> 16: <td align="right"><%= item.unit_price %></td> >> I know that to_f tries to make a variable into a float but I don''t >> know >> what is trying to do this. > > I''d guess it''s the multiplication that''s trying to coerce the object > types, > but that''s on line 17. > > In any case, I''d try to change your model: > > from... > item.quantity = 1 > > to... > item.quantity = 1.0 > > -- > -- Tom Mornini-- Posted via http://www.ruby-forum.com/.
What does the database schema look like for your line_items and products tables? -- Posted via http://www.ruby-forum.com/.
mysql> describe line_items; +------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | product_id | int(11) | | MUL | 0 | | | quantity | int(11) | | | 0 | | | unit_price | decimal(10,2) | | | 0.00 | | +------------+---------------+------+-----+---------+----------------+ mysql> describe products; +-------------+---------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------------------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | title | varchar(100) | | | | | | description | text | | | | | | image_url | varchar(200) | | | | | | price | decimal(10,2) | | | 0.00 | | | size | varchar(50) | | | | | | sold | enum(''current'',''gallery'') | | | current | | +-------------+---------------------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec) Jeremy Maziarz wrote:> What does the database schema look like for your line_items and products > tables?-- Posted via http://www.ruby-forum.com/.
I''ll bet if you looked at the class type for ''item.quantity'' and ''item.unit_price'' they would not be what you expect (fixnum and float respectively). Add these lines to your view: <%= debug(item.quantity.class) %><br/> <%= debug(item.unit_price.class) %> My test showed ''item.quantity'' as a fixnum but ''item.unit_price'' as a string. To fix it I converted ''item.unit_price'' to float: <%= item.unit_price.to_f * item.quantity %> -- Posted via http://www.ruby-forum.com/.
thanks for the help but still no headway, here is my out put now. What seems weird is the dump I get. The last section is a snippet of the session dump that looks odd to me. NoMethodError in Store#display_cart Showing app/views/store/display_cart.rhtml where line #14 raised: undefined method `to_f'' for #<Product:0xb79e9e40> Extracted source (around line #14): 11: product = item.product 12: -%> 13: <%= debug(item.quantity.class) %><br/> 14: <%= debug(item.unit_price.class) %> 15: <tr> 16: <td><%= item.quantity %></td> 17: <td><%= h(product.title) %></td> RAILS_ROOT: script/../config/.. usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1501:in `method_missing'' (eval):1:in `unit_price'' #{RAILS_ROOT}/app/views/store/display_cart.rhtml:14 #{RAILS_ROOT}/app/views/store/display_cart.rhtml:10 ##session dump !ruby/object:LineItem attributes: quantity: 1 product_id: 4 unit_price: &id002 !ruby/object:Product attributes: image_url: http://localhost:3000/images/twin_fairies_small.gif size: 12 inches by 18 inches Jeremy Maziarz wrote:> I''ll bet if you looked at the class type for ''item.quantity'' and > ''item.unit_price'' they would not be what you expect (fixnum and float > respectively). > > Add these lines to your view: > > <%= debug(item.quantity.class) %><br/> > <%= debug(item.unit_price.class) %> > > My test showed ''item.quantity'' as a fixnum but ''item.unit_price'' as a > string. To fix it I converted ''item.unit_price'' to float: > > <%= item.unit_price.to_f * item.quantity %>-- Posted via http://www.ruby-forum.com/.
I''ve narrowed it down a little. The page will display as long I don''t mention unit_price. For somereason that throws it off. -- Posted via http://www.ruby-forum.com/.
Turns out it was a bug in the built in server. I restarted the server and all my problems stopped. Thanks to all for thier advice! I think I''m going to like this rails stuff! charlie bowman wrote:> I''ve narrowed it down a little. The page will display as long I don''t > mention unit_price. For somereason that throws it off.-- Posted via http://www.ruby-forum.com/.
Thanks for posting the *unusual* solution to this Charlie. I just encountered the same problem. Sound advice worthy of fine-print note on the bottom of a page in ''Agile Web Development With Rails''. charlie bowman wrote:> Turns out it was a bug in the built in server. I restarted the server > and all my problems stopped. Thanks to all for thier advice! I think > I''m going to like this rails stuff!-- Posted via http://www.ruby-forum.com/.