# No method error - nil object

**URL:** <https://www.ruby-forum.com/t/no-method-error-nil-object/161864>\
**Category:** Rails\
**Created:** [March 3, 2009, 6:42am UTC](https://www.ruby-forum.com/t/no-method-error-nil-object/161864 "2009-03-03T06:42:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 3, 2009, 6:42am UTC](https://www.ruby-forum.com/t/no-method-error-nil-object/161864/1 "2009-03-03T06:42:17Z")

</div>

I am getting following error:  
##################################################  
NoMethodError in TopicsController#show  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.items  
##################################################

####################

## Show Method in topics controller

####################  
def show  
@topic = @object  
@items = @topic.items  
end

####################  
##Topic Model  
####################  
class Topic \< ActiveRecord::Base  
validates\_presence\_of :title, :section\_id  
validates\_uniqueness\_of :title  
has\_many :items  
:order =\> :position,  
:dependent =\> :destroy, #don’t leave orphans (delete my children)  
:conditions =\>“parent\_id is null” #  
belongs\_to :section  
end

####################  
##Item Model  
####################

class Item \< ActiveRecord::Base  
validates\_uniqueness\_of :title, :scope =\> [:topic\_id, :parent\_id]  
validates\_presence\_of :topic\_id, :title, :owner, :position

belongs\_to :topic  
belongs\_to :parent,  
:class\_name =\> “Item”,  
:foreign\_key =\> “parent\_id”  
has\_many :children,  
:class\_name =\> “Item”,  
:foreign\_key =\> “parent\_id”,  
:order =\> :position,  
:dependent =\> :destroy #don’t leave orphans (cascade delete)  
has\_many :attachments,  
:dependent =\> :destroy #don’t leave orphans (cascade delete)

def section  
self.topic ? self.topic.section : nil  
end  
end  
####################

In addition, I have Items controller and attachments controller. Right  
now I am starting with first Topics controller only. My config/routes.rb  
file contains:  
map.resources :topics  
. I would like to make this application RESTful.

Any ideas on how to proceed and resolve this error?

Thanks,  
CS.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 3, 2009, 7:25am UTC](https://www.ruby-forum.com/t/no-method-error-nil-object/161864/2 "2009-03-03T07:25:19Z")

</div>

def show  
@topic = Topic.find(params[:id])  
@items = @topic.items  
end  
Try this method

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 3, 2009, 8:04am UTC](https://www.ruby-forum.com/t/no-method-error-nil-object/161864/3 "2009-03-03T08:04:08Z")

</div>

Thanks…  
Well, I figured it out after I posted it on forum…  
While I know why passing parameter is working, I don’t understand why  
first approach didn’t work. Any clues?

Priya B. wrote:

> def show  
> @topic = Topic.find(params[:id])  
> @items = @topic.items  
> end  
> Try this method

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 3, 2009, 8:54am UTC](https://www.ruby-forum.com/t/no-method-error-nil-object/161864/4 "2009-03-03T08:54:55Z")

</div>

You didn’t mention the id of the topic which you are trying to show…  
Also where you have created @topic object?

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 3, 2009, 9:11am UTC](https://www.ruby-forum.com/t/no-method-error-nil-object/161864/5 "2009-03-03T09:11:57Z")

</div>

your first aproach was this:

def show  
@topic = @object  
@items = @topic.items  
end

where would @object come from. it’s a new uninitialized variable (=  
nil). that’s the reason it didn’t work.  
what else did you expect than nil.items ?
