Topic: NoMethodError: You have a nil object when you didn't expect it!

Could someone please help me with this problem:

ruby test/functional/admin/user_controller_test.rb 
Loaded suite test/functional/admin/user_controller_test
Started
.Fignoring attempt to close p with br
  opened at byte 80, line 6
  closed at byte 130, line 6
  attributes at open: {}
  text around open: "ethod=\"post\">\t\n\t\t\n\n\n<p><label for=\"user_"
  text around close: "\">First name</label></br>\n\t\n\t<input id=\""
ignoring attempt to close p with br
  opened at byte 222, line 10
  closed at byte 270, line 10
  attributes at open: {}
  text around open: "ype=\"text\" /></p>\n\t\n<p><label for=\"user_"
  text around close: "e\">Last name</label></br>\n\t\n\t<input id=\""
.test/functional/admin/user_controller_test.rb:78: warning: Object#id will be deprecated; use Object#object_id
E
Finished in 0.124222 seconds.

  1) Failure:
test_index(Admin::UserControllerTest)
    [/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:102:in `assert_tag'
     /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:99:in `clean_backtrace'
     /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:99:in `assert_tag'
     test/functional/admin/user_controller_test.rb:71:in `test_index'
     test/functional/admin/user_controller_test.rb:69:in `each'
     test/functional/admin/user_controller_test.rb:69:in `test_index']:
expected tag, but no tag found matching {:tag=>"td", :content=>"James"} in:
"<h1>View Users</h1>\n<p>From here you can view the users in the system.</p>\n<table>\n\t<tr>\n\t\t<th>Name</th>\n\t\t<th>Edit</th>\n\t\t<th>Delete</th>\n\t</tr>\n\t<tr>\n <td><a href=\"/admin/user/index/1?user=show\">James Apps</a></td>\n <td><a href=\"/admin/user/edit/1\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/1\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user James Apps?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr><tr>\n <td><a href=\"/admin/user/index/2?user=show\">Mark Faulkner</a></td>\n <td><a href=\"/admin/user/edit/2\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/2\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user Mark Faulkner?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr><tr>\n <td><a href=\"/admin/user/index/3?user=show\">Andy Jardine</a></td>\n <td><a href=\"/admin/user/edit/3\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/3\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user Andy Jardine?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr>\n</table>\n\n<p><a href=\"/admin/user/new\">Add a User</a></p>".
<nil> is not true.

  2) Error:
test_show(Admin::UserControllerTest):
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:447:in `extra_keys'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:447:in `map'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:447:in `extra_keys'
    generated code (/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:351):3:in `generate_extras'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1261:in `send'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1261:in `generate'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1260:in `each'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1260:in `generate'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1214:in `generate_extras'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1210:in `extra_keys'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/test_process.rb:102:in `assign_parameters'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/test_process.rb:377:in `process'
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/test_process.rb:353:in `get'
    test/functional/admin/user_controller_test.rb:78:in `test_show'

4 tests, 11 assertions, 1 failures, 1 errors


user_controller_test.rb
 def test_show
    get :show, id => 1  # Request the show page
    assert_template 'admin/user/show' # Test the right template is called
    # assigns(). helper method Checks instance variable to see if its the right one
    assert_equal 'James', assigns(:user).first_name
    assert_equal 'James', assigns(:user).last_name
  end

usrs.rb
james_apps:
  id: 1
  first_name: James
  last_name: Apps
Mark_faulkner:
  id: 2
  first_name: Mark
  last_name: Faulkner
andy_jardine:
  id: 3
  first_name: Andy
  last_name: Jardine

Thank you

Re: NoMethodError: You have a nil object when you didn't expect it!

You need to make "id" a symbol:

get :show, :id => 1  # Request the show pag

Railscasts - Free Ruby on Rails Screencasts

Re: NoMethodError: You have a nil object when you didn't expect it!

Thank you that works now!

Re: NoMethodError: You have a nil object when you didn't expect it!

no it didnt, sorry.

 1) Failure:
test_index(Admin::UserControllerTest)
    [/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:102:in `assert_tag'
     /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:99:in `clean_backtrace'
     /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:99:in `assert_tag'
     test/functional/admin/user_controller_test.rb:71:in `test_index'
     test/functional/admin/user_controller_test.rb:69:in `each'
     test/functional/admin/user_controller_test.rb:69:in `test_index']:
expected tag, but no tag found matching {:tag=>"td", :content=>"James"} in:
"<h1>View Users</h1>\n<p>From here you can view the users in the system.</p>\n<table>\n\t<tr>\n\t\t<th>Name</th>\n\t\t<th>Edit</th>\n\t\t<th>Delete</th>\n\t</tr>\n\t<tr>\n <td><a href=\"/admin/user/index/1?user=show\">James Apps</a></td>\n <td><a href=\"/admin/user/edit/1\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/1\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user James Apps?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr><tr>\n <td><a href=\"/admin/user/index/2?user=show\">Mark Faulkner</a></td>\n <td><a href=\"/admin/user/edit/2\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/2\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user Mark Faulkner?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr><tr>\n <td><a href=\"/admin/user/index/3?user=show\">Andy Jardine</a></td>\n <td><a href=\"/admin/user/edit/3\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/3\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user Andy Jardine?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr>\n</table>\n\n<p><a href=\"/admin/user/new\">Add a User</a></p>".
<nil> is not true.

  2) Failure:
test_show(Admin::UserControllerTest) [test/functional/admin/user_controller_test.rb:82]:
<"James"> expected but was
<"Apps">.

4 tests, 14 assertions, 2 failures, 0 errors

Re: NoMethodError: You have a nil object when you didn't expect it!

Those are failures which are different than errors. The test_show failure is simply because it is returning the last_name properly, but you are checking the first name. Try this:

 def test_show
    get :show, id => 1  # Request the show page
    assert_template 'admin/user/show' # Test the right template is called
    # assigns(). helper method Checks instance variable to see if its the right one
    assert_equal 'James', assigns(:user).first_name
    assert_equal 'Apps', assigns(:user).last_name
  end

I can't tell what the problem with test_index is without the code.

Railscasts - Free Ruby on Rails Screencasts

Re: NoMethodError: You have a nil object when you didn't expect it!

Don't worry I fixed it I was being stupid I didn't fulfil the text case.

Re: NoMethodError: You have a nil object when you didn't expect it!

hey i got the same problem. Is ryanb ryan bates? if you are, I watched your tutorials, and I'm trying to hook up posts with users, using the has_many and belongs_to relationship.

Im using restful_authentication, and I wasn't sure how I would link posts with users. I want each user to have their own posts... I tried following ur 15 min ror beginners tutorial, but I get this nill object error.

"You have a nil object when you didn't expect it!
The error occurred while evaluating nil.posts"

I did rake routes, and everything seems to be inplace. hm.... is there a solution? thanks

Re: NoMethodError: You have a nil object when you didn't expect it!

That code is too confusing )
astigmatism

Last edited by nikozan (2011-06-25 03:21:52)