SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

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

    Agile WebDev Cart Display Error

    i'm working through the cart example in agile webdev with rails and i've hit a snag.

    i was able to get the cart to display the quantity of products selected via the stub.

    however, when i was supposed to update the stub (after updating the ApplicationController to see the cart and line_item models), i got an error message.

    i double checked my code and found an error, but it didn't ultimately resolve the issue.

    the error i'm getting is...

    Code:
     NoMethodError in Store#display_cart
    
    Showing app/views/store/display_cart.rhtml where line #10 raised:
    
    You have a nil object when you didn't expect it!
    The error occured while evaluating nil.title
    
    Extracted source (around line #10):
    
    7: -%>
    8:   <tr>
    9:     <td><%= item.quantity %></td>
    10:     <td><%= h(product.title) %></td>
    11:     <td align="right"><%= item.unit_price %></td>
    12:     <td align="right"><%= item.unit_price * item.quantity %></td>
    13:   </tr>
    
    RAILS_ROOT: script/../config/..
    Application Trace | Framework Trace | Full Trace
    
    #{RAILS_ROOT}/app/views/store/display_cart.rhtml:10:in `_run_rhtml_store_display_cart'
    #{RAILS_ROOT}/app/views/store/display_cart.rhtml:5:in `_run_rhtml_store_display_cart'
    
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in `compile_and_render_template'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in `render_template'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in `render_file'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in `render_file'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in `render_with_no_layout'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:243:in `render_without_benchmark'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:911:in `perform_action_without_filters'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in `handle_dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in `service'
    /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
    /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
    /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in `dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
    /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
    /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
    script/server:3
    
    #{RAILS_ROOT}/app/views/store/display_cart.rhtml:10:in `_run_rhtml_store_display_cart'
    #{RAILS_ROOT}/app/views/store/display_cart.rhtml:5:in `_run_rhtml_store_display_cart'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in `compile_and_render_template'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in `render_template'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in `render_file'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in `render_file'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in `render_with_no_layout'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:243:in `render_without_benchmark'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:911:in `perform_action_without_filters'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in `handle_dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in `service'
    /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
    /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
    /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in `dispatch'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
    /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
    /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
    /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
    script/server:3
    
    Request
    
    Parameters: None
    
    Show session dump
    
    --- 
    :cart: !ruby/object:Cart 
      items: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
        product: 
      - !ruby/object:LineItem 
        attributes: 
          quantity: 0
          product_id: 
          unit_price: 
        new_record: true
      - !ruby/object:LineItem 
        attributes: 
          quantity: 1
          product_id: 3
          unit_price: 24.95
        new_record: true
        product: !ruby/object:Product 
          attributes: 
            image_url: /images/ror.png
            date_available: "2006-05-20"
            price: "24.95"
            title: Agile Web Development with Rails
            id: "3"
            description: This book describes how to program in an agile manner using Ruby on Rails.  Ruby is an object oriented programming language and Rails is a framework.  It takes a while to convert from procedural thinking, but, once you do, application development becomes much more efficient.
      - !ruby/object:LineItem 
        attributes: 
          quantity: 1
          product_id: 2
          unit_price: 10.0
        new_record: true
        product: !ruby/object:Product 
          attributes: 
            image_url: /images/sk_auto_big.jpg
            date_available: "2006-05-13"
            price: "10.00"
            title: Product 2
            id: "2"
            description: Product 2
      - !ruby/object:LineItem 
        attributes: 
          quantity: 1
          product_id: 2
          unit_price: 10.0
        new_record: true
        product: !ruby/object:Product 
          attributes: 
            image_url: /images/sk_auto_big.jpg
            date_available: "2006-05-13"
            price: "10.00"
            title: Product 2
            id: "2"
            description: Product 2
      total_price: 284.6
    flash: !map:ActionController::Flash::FlashHash {}
    
    
    Response
    Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}
    i would appreciate any help to resolve this isssue.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is that product is nil (not set) in this line:

    Code:
    <td><%= h(product.title) %></td>
    Maybe you meant @product.title, or you've mistyped the name in the loop?

    Could you post you view + controller code if this doesn't resolve your issue?

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where does the product variable come from? Could you post the entire code of display_cart.rhtml?

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    Where does the product variable come from? Could you post the entire code of display_cart.rhtml?
    here's the cart.rhtml code:

    Code:
    <h1>Display Cart</h1>
    
    <table>
    <% 
    for item in @items 
      product = item.product
    -%>
      <tr>
        <td><%= item.quantity %></td>
        <td><%= h(product.title) %></td>
        <td align="right"><%= item.unit_price %></td>
        <td align="right"><%= item.unit_price * item.quantity %></td>
      </tr>
    <% end -%>
    </table>
    i think that matches the book code

    this stub code does work and displays fine:

    Code:
    <p>Your cart contains <%= @items.size %> items</p>

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [edited - solved. i allow cookies, but have some exceptions. localhost and booksprag where exceptions that i don't recall setting. it all works now]

    i think i resolved the issue, but was unaware of it due to not clearing out my cache, cookies and/or restarting webrick.

    in any case, using firefox, it now displays "Display Cart" and nothing else.

    however, when i run konquerer, it works as advertized.

    this is frustrating. i've cleared out the cache and cleared out cookies (localhost and books pragprog). still, the table (actually, for loop) doesn't display in firefox, but does in konquerer.

    why would it display in konquerer and not in firefox? in ff, i can print before and after the loop... but it doesn't recognize the table loop.

    Code:
    <h1>Display Cart</h1>
    ff - this prints to screen
    <table>
    <% 
    for item in @items 
      product = item.product
    -%>
      <tr>
        <td><%= item.quantity %></td>
        <td><%= h(product.title) %></td>
        <td align="right"><%= item.unit_price %></td>
        <td align="right"><%= item.unit_price * item.quantity %></td>
      </tr>
    <% end -%>
    ff - this prints to screen
    </table>
    Last edited by skeeterbug; May 21, 2006 at 15:45.


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
  •