Nil object in E1 capture the order

I’m following the depot application in the rails bible Agile Web
Development with Rails. In interation E1

NoMethodError in Admin#checkout

Showing app/views/admin/checkout.rhtml where line #12 raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.include?

Extracted source (around line #12):

9: <% form_for :list, :url => { :action => :save_list } do |form| %>
10:


11: List Name:
12:
13:

I have a page with all the product listed. And I’m creating the list by
choosing from products on this page.
For database and model setting I have
product.rb
has many list_item

list.rb
has many list_item

list_item.rb

class ListItem

#belongs_to :list
#belongs_to :product
include Reloadable
attr_reader :product, :quantity

The lines I commented out will generate errors as follows

NoMethodError

undefined method `belongs_to’ for ListItem:Class

RAILS_ROOT: C:/INSTAN~1/rails_apps/test/config/…
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/models/list_item.rb:4
#{RAILS_ROOT}/app/controllers/application.rb:8

This error occured while loading the following files:
application.rb
=> model list_item.rb
list_item.rb

I think this is the root of the nil object problem in checkout.
Since line_item is not correctly related to list and product.
So the list is a nil object.

Anyone knows why "belongs to " is not recognized?

Thanks in advance

class ListItem

#belongs_to :list
#belongs_to :product
include Reloadable
attr_reader :product, :quantity

ListItem needs to extend ActiveRecord::Base

class ListItem < ActiveRecord::Base
belongs_to :list
belongs_to :product

...

end

Snowman wrote:

class ListItem

#belongs_to :list
#belongs_to :product
include Reloadable
attr_reader :product, :quantity

ListItem needs to extend ActiveRecord::Base

class ListItem < ActiveRecord::Base
belongs_to :list
belongs_to :product

...

end

Thanks! belongs_to is recognized.
But the nil object problem still remains.
My reasoning about the “belongs_to unrecognition” to “nil object” seems
to be wrong.

Followings are my the action capture the list item.

def add_to_list

begin

@product = Product.find(params[:id])
rescue
logger.error(“Attempt to access invalid product #{params[:id]}”)
flash[:notice] = “There is no such product”
redirect_to :action => :index
else
@list = find_list
@current_item= @list.add_product(@product)
redirect_to_index unless request.xhr?
end
end

def checkout
@list = find_list
if @list.items.empty?
redirect_to_index(“Your list is empty”)
else
@list = List.new
end
end
def empty_list

session[:list] =nil
redirect_to_index
end

def checkout
@list = find_list
if @list.items.empty?
redirect_to_index(“Your list is empty”)
else
@list = List.new
end
end

My guess is that find_list is returning nil and you’re getting an error
when you call @list.items. If this is not it then I’ll need more
information - do you have a stack trace?

Do you mean this?

Yes, that’s exactly what I meant.

The line of code in rails that is causing the problem is this:

    if @attributes.include?(method_name) or

in ActiveRecord::Base

This indicates that some how the @attributes instance variable has been
set to nil in one of your models. Are you directly using an instance
variable with this name in one of your models?

Snowman wrote:

def checkout
@list = find_list
if @list.items.empty?
redirect_to_index(“Your list is empty”)
else
@list = List.new
end
end

My guess is that find_list is returning nil and you’re getting an error
when you call @list.items. If this is not it then I’ll need more
information - do you have a stack trace?

Do you mean this?

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.include?

Extracted source (around line #12):

9: <% form_for :list, :url => { :action => :save_list } do |form| %>
10:


11: 皜?迂:
12:
13:


14:
15:

RAILS_ROOT: C:/INSTAN~1/rails_apps/test/config/…

Application Trace | Framework Trace | Full Trace
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1772:in
method_missing' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:341:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:341:in
value_before_type_cast' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:253:into_input_field_tag’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:160:in
text_field' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:395:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:395:in
text_field' #{RAILS_ROOT}/app/views/admin/checkout.rhtml:12:in_run_rhtml_admin_checkout’
#{RAILS_ROOT}/app/views/admin/checkout.rhtml:9:in
_run_rhtml_admin_checkout' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1772:inmethod_missing’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:341:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:341:invalue_before_type_cast’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:253:in
to_input_field_tag' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:160:intext_field’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:395:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:395:intext_field’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:149:in
fields_for' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:127:inform_for’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:incompile_and_render_template’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:in
render_template' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:inrender_file’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:726:in
render_file' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:648:inrender_with_no_layout’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/layout.rb:245:in
render_without_benchmark' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:inrender’
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:inrender’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:942:in
perform_action_without_filters' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:inperform_action_without_benchmark’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:inperform_action’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:inprocess_without_filters’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in
process_without_session_management_support' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:inprocess’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in
dispatch' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/rails.rb:73:inprocess’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:551:in
process_client' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:ineach’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in
process_client' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in
initialize' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:innew’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:ininitialize’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in
new' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:956:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:ineach’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:127:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/command.rb:199:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:235 C:/INSTAN~1/ruby/bin/mongrel_rails:18:inload’
C:/INSTAN~1/ruby/bin/mongrel_rails:18
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1772:in
method_missing' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:341:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:341:in
value_before_type_cast' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:253:into_input_field_tag’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:160:in
text_field' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:395:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:395:in
text_field' #{RAILS_ROOT}/app/views/admin/checkout.rhtml:12:in_run_rhtml_admin_checkout’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:149:in
fields_for' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/form_helper.rb:127:inform_for’
#{RAILS_ROOT}/app/views/admin/checkout.rhtml:9:in
_run_rhtml_admin_checkout' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:in
compile_and_render_template' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:inrender_template’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:in
render_file' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:726:inrender_file’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:648:in
render_with_no_layout' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/layout.rb:245:inrender_without_benchmark’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:in
render' C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:in
render' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:942:inperform_action_without_filters’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in
perform_action_without_benchmark' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in
perform_action' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in
process_without_filters' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:inprocess_without_session_management_support’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in
process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:indispatch’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/rails.rb:73:in
process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:551:inprocess_client’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in
each' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:inprocess_client’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:ininitialize’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in
new' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in
initialize' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:innew’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:956:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in
each' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:127:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/command.rb:199:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:235
C:/INSTAN~1/ruby/bin/mongrel_rails:18:in `load’
C:/INSTAN~1/ruby/bin/mongrel_rails:18
Request
Parameters: None

Show session dump


:user_id: 1
:list: !ruby/object:List
items:

  • !ruby/object:ListItem
    product: !ruby/object:Product
    attributes:
    popular_score:
    title: !binary |
    5LiJ6YOO6aSQ5YyF

    nickname:
    primary_price: "30"
    primary_link: http://tw.yahoo.com
    id: "1"
    image_url1: http://pandora.com/images/logo_pandora.gif
    description: !binary |
      6auY6ZuE5ZCN55Si
    
    user_id: "4"
    created_at: 0000-00-00 00:00:00
    browse_times:
    

    quantity: 1

  • !ruby/object:ListItem
    product: !ruby/object:Product
    attributes:
    popular_score:
    title: Chanel patent- leather high- heels
    nickname:
    primary_price: “500”
    primary_link: http://www.highheelshoemuseum.com/
    id: “3”
    image_url1:
    http://ec3.images-amazon.com/images/P/B000H6QZLA.01-A242Z34VYGHM9W.AA280_SCLZZZZZZZ_V62233886.jpg
    description: bold red and patent red are this season’s must-buy
    user_id: “4”
    created_at: 0000-00-00 00:00:00
    browse_times:
    quantity: 1

  • !ruby/object:ListItem
    product: !ruby/object:Product
    attributes:
    popular_score:
    title: “\xE5\xB9\xB3\xE5\xBA\x95\xE5\x9C\x93\xE9
    \xAD\xE8\x8A\xAD\xE8\x95\xBE\xE9\x9E\x8B”
    nickname:
    primary_price: “690”
    primary_link: http://tw.page.bid.yahoo.com/tw/auction/1148041732
    id: “4”
    image_url1:
    http://tw.image.bid.yahoo.com/users/4/8/1/3/titi811019-img600x300-1162618416nn21-4.jpg
    description:
    "\xE4\xB8\x80\xE7\x9B\xB4\xE5\xBE\x88\xE6\x83\xB3\xE5?\x9A\xE5\x83?French
    Sole. London
    Sole\xE9\x82\xA3\xE6\xA8\xA3\xE7\x9A\x84\xE8\x8A\xAD\xE8\x95\xBE\xE9\x9E\x8B\xE6\xAC\xBE!\r\n
    \xE7\xB1\x8C\xE5\x82\x99\xE5\xBE\x88\xE4\xB9\x85\xE7\xB5\x82\xE6\x96\xBC\xE4\xB8\x8A\xE6\x9E\xB6\xE5\x9B\x89^^\r\n
    \xE9\x9E\x8B\xE5\xBA\x95\xE7\x89\xB9\xE5\x88\xA5\xE4\xBD\xBF\xE7\x94\xA8\xE9\x98\xB2\xE6\xBB\x91\xE7\x9A\x84\xE6\xA9\xA1\xE8\x86
    \xE5\xBA\x95~\r\n
    \xE5\x8A
    \xE4\xB8\x8A\xE5\x8E\x9A\xE5\x8E\x9A\xE7\x9A\x84\xE6\xB0\xA3\xE5\xA2\x8A\xE9\x9E\x8B\xE5\xBA\x95~\r\n
    \xE7\xA9\xBF\xE8\xB5\xB7\xE4\xBE\x86\xE5\x83?\xE9?\x8B\xE5\x8B\x95\xE9\x9E\x8B\xE4\xB8\x80\xE6\xA8\xA3\xE8\x88\x92\xE6\x9C?\xE5\x96\x94!\r\n
    \r\n
    \xE6\xAF?\xE9\x9B\x99\xE9\x83\xBD\xE6?\xAD\xE9\x85?\xE4\xB8?\xE5?\x8C\xE7\x9A\x84\xE8?\xB4\xE8?\xB6\xE7\xB5?\xE5\x92\x8C\xE8\xBB\x8A\xE9\x82\x8A.\xE8\xB6\x85\xE7\xB4\x9A\xE5?\xAF\xE6\x84\x9B\xE5\x96\x94!\r\n
    \xE6\x8E\xA8\xE5\x87\xBA\xE7\xA7\x8B\xE5\x86\xAC\xE6\x96\xB0\xE8\x89\xB2!\xE6\xBC\x86\xE7\x9A\xAE\xE6\xAC\xBE!\xE8\xB1?\xE5\xAF\x8C\xE7\x9A\x84\xE7\xB3\x96\xE6\x9E\x9C\xE8\x89\xB2\xE5\xBD\xA9!\xE7\x9C\x8B\xE4\xBA\x86\xE5\xB0\xB1\xE8\xB6\x85\xE7\xB4\x9A\xE5\x96\x9C\xE6\xAD\xA1^^\r\n
    \xE6\x96\xB0\xE5\xA2\x9E\xE5\xA4\xA7\xE5\xB0\xBA\xE7\xA2\xBC43~45!\xE6\x95\xB8\xE9\x87?\xE4\xB8?\xE5\xA4\x9A\xE5\x96\x9C\xE6\xAD\xA1\xE8\xA6?\xE6\x8A\x8A\xE6?\xA1\xE5\x96\x94^^
    "
    user_id: “4”
    created_at: 0000-00-00 00:00:00
    browse_times:
    quantity: 1

  • !ruby/object:ListItem
    product: !ruby/object:Product
    attributes:
    popular_score:
    title: “ZARA
    \xE7\xB7\x9E\xE5\xB8\xB6\xE5\x84\xAA\xE9\x9B\x85\xE6\xAC\xBE\xE5\x9C\x93\xE9\xA0\xAD\xE5\xB9\xB3\xE5\xBA\x95\xE9\x9E\x8B”
    nickname:
    primary_price: “3450”
    primary_link: http://tw.page.bid.yahoo.com/tw/auction/1148216341
    id: “5”
    image_url1:
    http://tw.image.bid.yahoo.com/users/0/3/2/9/princesskkk313-img480x360-11663084155-10.jpg
    description: "ZARA
    \xE8\xA5\xBF\xE7\x8F\xAD\xE7\x89\x99\xE6\x9C\x8D\xE9\xA3\xBE\r\n
    \r\n
    \xE9\xA6\x99\xE5\xA5\x88\xE5\x85\x92\xE6\x9C\x89\xE4\xB8\x80\xE6\xAC\xBE\xE8\xB6\x85\xE5\x83\x8F\xE9\x80\x99\xE5\x80\x8B\xE7\x9A\x84\r\n
    \r\n
    \xE5\xBE\x88\xE5\x84\xAA\xE9\x9B\x85\xE7\x9A\x84\xE4\xB8\x80\xE6\xAC\xBE\xE7\xA9\xBF\xE8\xB5\xB7\xE4\xBE\x86\xE5\xBE\x88\xE6\x9C\x89\xE6\xB0\xA3\xE8\xB3\xAA\xE6\xAD\x90
    ^O^\r\n
    \r\n
    \xE6\x90\xAD\xE9\x85\x8D\xE6\x88\x91\xE9\x85\x8D\xE6\x88\x91\xE8\xB3\xA3\xE5\xA0\xB4\xE7\x9A\x84\xE9\xA6\x99\xE5\xA5\x88\xE5\x85\x92\xE5\x8C\x85\xE8\xB6\x85\xE5\xA5\xBD\xE7\x9C\x8B\xE7\x9A\x84\xEF\xBC\x81\xEF\xBC\x81
    \r\n
    \r\n
    \xE5\x89\x9B\xE4\xB8\x8A\xE5\xB8\x82\xE5\xB0\xB1\xE8\xA2\xAB\xE6\x90\xB6\xE8\xB3\xBC\xE4\xB8\x80\xE7\xA9\xBA\xE4\xBA\x86!\r\n
    \r\n
    \xE4\xB8\x8D\xE7\xA2\xBA\xE5\xAE\x9A\xE9\x82\x84\xE6\x9C\x89\xE6\xB2\x92\xE6\x9C\x89\xE8\xB2\xA8\xE5\xA6\x82\xE6\x9E\x9C\xE8\xA6\x81\xE7\x9A\x84\xE8\xA9\xB1\xE5\x85\x88\xE8\xB7\x9F\xE5\x87\xB1\xE8\xA5\xBF\xE8\xAA\xAA\xE6\x88\x91\xE6\x89\xBE\xE5\x88\xB0\xE5\x9C\xA8\xE4\xB8\x8B\xE6\xA8\x99\xE6\xAD\x90!
    \r\n
    \r\n \r\n
    \r\n
    \xE7\x9B\xAE\xE5\x89\x8D\xE6\x9C\x89\xE8\xB3\xBC\xE8\xB2\xB7\xE8\xB5\xB7\xE4\xBE\x86\xE7\x9A\x84\xE6\x9C\x89
    36 \xE8\x99\x9F\xEF\xBC\x81\r\n
    \r\n
    \xE8\xB3\xA3\xE5\xA0\xB4\xE9\x82\x84\xE6\x9C\x89\xE7\xB1\xB3\xE8\x89\xB2\xE7\x9A\x84\xE4\xB9\x9F\xE8\xB3\xA3\xE7\x9A\x84\xE8\xB6\x85\xE7\xB4\x9A\xE5\xA5\xBD\xE4\xBA\x86\xEF\xBC\x81\xEF\xBC\x81
    "
    user_id: “1”
    created_at: 0000-00-00 00:00:00
    browse_times:
    quantity: 1
    flash: !map:ActionController::Flash::FlashHash {}

Thanks

Snow Man wrote:

Do you mean this?

Yes, that’s exactly what I meant.

The line of code in rails that is causing the problem is this:

    if @attributes.include?(method_name) or

in ActiveRecord::Base

This indicates that some how the @attributes instance variable has been
set to nil in one of your models. Are you directly using an instance
variable with this name in one of your models?

I ended up finding out that I’m mistaking line_item.rb with
cart_item.rb.
I followed the book again. But this time I got an application error
which is almost clueless.T T

It’s really hard to figure out what’s going wrong.

Abon wrote:

Snow Man wrote:

Do you mean this?

Yes, that’s exactly what I meant.

The line of code in rails that is causing the problem is this:

    if @attributes.include?(method_name) or

in ActiveRecord::Base

This indicates that some how the @attributes instance variable has been
set to nil in one of your models. Are you directly using an instance
variable with this name in one of your models?

I ended up finding out that I’m mistaking line_item.rb with
cart_item.rb.
I followed the book again. But this time I got an application error
which is almost clueless.T T

It’s really hard to figure out what’s going wrong.

There is what I digged out from my development.log

Session contains objects whose class definition isn’t available.
Remember to require the classes for all objects kept in the session.
(Original exception: uninitialized constant ListItem [NameError])

And I also changed

def checkout
@list = find_list
puts @list.inspect
if @list.items.empty?
redirect_to_index(“Your list is empty”)
else
@list = List.new
end
end

in the server log

the @list contains list item which is definitely not empty.

It shows like this

#<List:0x46cee3c @items=[#<ListItem:0x46cedd8 @quantity=1,
@product=#<Product:0x46ced60 @attributes={“popular_score”=>nil, “t
in Alta velvet knee high boots - NET-A-PORTER.COM”, “nickname”=>nil,
“primary_price”=>“49343”, “primary_link”=>“http://www.ne
983”, “id”=>“29”,
“image_url1”=>“http://www.net-a-porter.com/intl/images/product/15983/large/index.jpg”,
“description”=>“Blac
with 110mm silver Swarovski rhinestone encrusted heel. Christian
Louboutin boots have a pointed toe, a zip fastening on inner
sole.”, “user_id”=>“1”, “created_at”=>“2007-01-12 17:50:51”,
“browse_times”=>nil}>>, #<ListItem:0x46ce7d4 @quantity=1, @produ
ttributes={“popular_score”=>nil, “title”=>“Christian Louboutin - Raffia
Slingback - Neiman Marcus”, “nickname”=>nil, “prima
ary_link”=>“http://www.neimanmarcus.com/store/catalog/prod.jhtml?itemId=prod35000031&parentId=cat5130731&masterId=cat000199&i
000141cat000149cat000199cat5130731”, “id”=>“35”,
“image_url1”=>“http://www.neimanmarcus.com/products/mn/NMX0174_mn.jpg”,
“des
Brown raffia.\r\n\342\200\242 Tonal leather piping.\r\n\342\200\242 Peep
toe.\r\n\342\200\242 Elastic-back slingback.\r\n\342
\n\342\200\242 4 4/5” heel.\r\n\342\200\242 Signature red
sole.\r\n\342\200\242 Made in Italy. ", “user_id”=>“1”, "created_a
, “browse_times”=>nil}>>]>

I ended up finding out that I’m mistaking line_item.rb with
cart_item.rb.
I followed the book again. But this time I got an application error
which is almost clueless.T T

It’s really hard to figure out what’s going wrong.

This is what I digged out from my development.log

Session contains objects whose class definition isn’t available.
Remember to require the classes for all objects kept in the session.
(Original exception: uninitialized constant ListItem [NameError])
I’ve searched ListItem under the application directory. Not found.
And I also cleared the sessions.
Still the same application error.
Where does Mongrel knows about ListItem?