Forum: Ruby on Rails learning to degug? where do I start

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.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-14 05:48
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.
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2005-12-14 06:09
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.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-14 06:12
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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2005-12-14 08:58
(Received via mailing list)
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
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-14 21:41
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
E5addfb886fa24edf1fcf9473a9b5a34?d=identicon&s=25 Jeremy Maziarz (jmaziarz)
on 2005-12-14 21:51
What does the database schema look like for your line_items and products
tables?
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 charie bowman (Guest)
on 2005-12-14 22:25
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?
E5addfb886fa24edf1fcf9473a9b5a34?d=identicon&s=25 Jeremy Maziarz (jmaziarz)
on 2005-12-14 23:19
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 %>
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-15 00:13
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 %>
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-15 00:15
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.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 charlie bowman (Guest)
on 2005-12-15 03:58
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.
4753e77234f0a37028d78517d1af06d5?d=identicon&s=25 Matthew Ueckerman (dueckes)
on 2006-01-03 13:29
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!
This topic is locked and can not be replied to.