SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] Agile WebDev Cart Issue (Displaying Total Price)

    i am getting an error message that has me stumped. i cleared my cart, i cleared my cookies (which destroyed my original post and forced me to relogin here - ugggh!), i verified the code three times and it matches the book.

    i'm at a loss.

    here goes:

    error message:
    Code:
    undefined method `total_price' for #<Cart:0xb7898594>
    ...
    NoMethodError in Store#add_to_cart
    ...
    12:     <td class="total-cell" ><%= number_to_currency(@cart.total_price) %></td>
    the problem i'm having is that def total_price is in cart.rb.

    cart.rb
    Code:
    class Cart
    
      attr_reader :items
    
      def initialize
        @items = []
      end
    
      def add_product(product)
        current_item = @items.find {|item| item.product == product}
        if current_item
          current_item.increment_quantity
        else
          @items << CartItem.new(product)
        end
      end
    
      def total_price
        @items.sum { |item| item.price }
      end
    
    end
    add_to_cart.rhtml
    Code:
    <div class="cart-title" >Your Cart</div>
    <table>
      <% for cart_item in @cart.items %>
        <tr>
          <td><%= cart_item.quantity %>&times;</td>
          <td><%= h(cart_item.title) %></td>
          <td class="item-price" ><%= number_to_currency(cart_item.price) %></td>
        </tr>
      <% end %>
      <tr class="total-line" >
        <td colspan="2" >Total</td>
        <td class="total-cell" ><%= number_to_currency(@cart.total_price) %></td>
      </tr>
    </table>
    <%= button_to "Empty cart" , :action => :empty_cart %>
    the offending code is
    Code:
    @cart.total_price
    if i remove it, the script runs, but, obviously, the total price doesn't display.

    if it helps the debug process, i'm using the price_in_cents plugin.

    i'm stumped - can anyone point me in the right direction?

    as always, tia...

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Did you reload the cart model after you made the change? Non-ActiveRecord models don't automatically reload from my understanding.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia View Post
    Did you reload the cart model after you made the change? Non-ActiveRecord models don't automatically reload from my understanding.
    vgarcia, i'm not sure how to reload the cart model so i stopped webrick and restarted it. will that reload the cart model?

    after doing so, i'm receiving a new error message:

    Code:
    undefined method `sum' for #<Array:0xb793d290>
    cart.rb
    Code:
    class Cart
    
      attr_reader :items
    
      def initialize
        @items = []
      end
    
      def add_product(product)
        current_item = @items.find {|item| item.product == product}
        if current_item
          current_item.increment_quantity
        else
          @items << CartItem.new(product)
        end
      end
    
      def total_price
        @items.sum { |item| item.price }
      end
    
    end
    add_to_cart.rhtml
    Code:
    <div class="cart-title" >Your Cart</div>
    <table>
      <% for cart_item in @cart.items %>
        <tr>
          <td><%= cart_item.quantity %>&times;</td>
          <td><%= h(cart_item.title) %></td>
          <td class="item-price" ><%= number_to_currency(cart_item.price) %></td>
        </tr>
      <% end %>
      <tr class="total-line" >
        <td colspan="2" >Total</td>
        <td class="total-cell" ><%= number_to_currency(@cart.total_price) %></td>
      </tr>
    </table>
    <%= button_to "Empty cart" , :action => :empty_cart %>
    i'm thinking that maybe the price_in_cents plugin requires slightly different code, but i'm not sure what that would be. "price" is the price in dollars and "price_in_cents" is the price in cents.

    btw, how does one reload a model within webrick (assuming it was loaded when i restarted webrick)?

    as always, tia...

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now that i got a chance to search the error, thi s code fails b/c i'm not running rails 1.2 via edge rails.

    vgarcia, thanks for the tip to reload the non-db model so i could get the right error message. i'm surprised the others missed mentioning this. either that or i missed reading it..

    i think the code is code and can't debug it until 1.2 comes out or until i go with edge rails.

  5. #5
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    skeeterbug, I have updated to Rails 1.2 but still have the same issue you have. I am using Ruby 1.8.5, does anyone know if that may be the problem? The second edition of "Agile Web Dev with Rails" recommended using 1.8.4. Also, I used InstantRails to setup my environment and then updated to Rails 1.2 (beta)

    What is more, when I look in the Ruby Documentation I don't see a "sum" method for Array...am I correct? That may explain why we are both getting the error:
    "undefined method `sum' for #<Array:0xb793d290>" :-)

    See if you can wade through this fix dated 05/03/2006 by Ron Jefferies who says "Of course there is no sum method in Array..." about a third the way down the page:
    http://www.xprogramming.com/xpmag/dbcBowlingVector.htm

    But in the meantime, here is my -- rather naive -- temporary fix, but it works. In the Cart class, modify the the def total_price method...

    FROM:
    def total_price
    @items.sum { |item| item.price }
    end

    TO:

    def total_price
    sum = 0
    for item in @items
    sum += Float(item.price)
    end
    return sum
    end


    I also tried:

    def total_price
    @items.inject(0) { |sum, item.price| sum + item.price}
    end

    but got the error: "CartItem can't be coerced into Fixnum"
    If anyone knows how to resolve this please let me know.

    Thanks
    Last edited by johnroodt; Dec 31, 2006 at 19:28.

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnroodt View Post
    skeeterbug, I have updated to Rails 1.2 but still have the same issue you have. I am using Ruby 1.8.5, does anyone know if that may be the problem? The second edition of "Agile Web Dev with Rails" recommended using 1.8.4. Also, I used InstantRails to setup my environment and then updated to Rails 1.2 (beta)

    What is more, when I look in the Ruby Documentation I don't see a "sum" method for Array...am I correct? That may explain why we are both getting the error:
    "undefined method `sum' for #<Array:0xb793d290>" :-)

    See if you can wade through this fix dated 05/03/2006 by Ron Jefferies who says "Of course there is no sum method in Array..." about a third the way down the page:
    http://www.xprogramming.com/xpmag/dbcBowlingVector.htm

    But in the meantime, here is my -- rather naive -- temporary fix, but it works. In the Cart class, modify the the def total_price method...

    FROM:
    def total_price
    @items.sum { |item| item.price }
    end

    TO:

    def total_price
    sum = 0
    for item in @items
    sum += Float(item.price)
    end
    return sum
    end


    I also tried:

    def total_price
    @items.inject(0) { |sum, item.price| sum + item.price}
    end

    but got the error: "CartItem can't be coerced into Fixnum"
    If anyone knows how to resolve this please let me know.

    Thanks
    i recommend going with ruby 1.84 if it is recommended. it is recommended for a reason and will likely bite you later if it isn't biting you now.

    this may solve the issue.

    good luck.

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] Rails View Date Entry Code

    how does rails handle displaying dates in views?

    scaffolding makes this behavior very easy, but i need to manually code the view and i'm not sure how to do it.

    iow, i want to allow the user to enter a date and i'm guessing there is a rails way to do it.

    if not, do i have to use the select - option method? if so, how do i set the default option values for year, month and day?

    tia...

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do `date_select :variable, :attribute` or if you use form_for: `form.date_select :attribute`.

    Edit:

    def total_price
    @items.inject(0) { |sum, item.price| sum + item.price}
    end

    but got the error: "CartItem can't be coerced into Fixnum"
    If anyone knows how to resolve this please let me know.
    {|sum, item.price| should be {|sum, item|.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •