# NoMethodError in Book#show\_subjects

**URL:** <https://www.ruby-forum.com/t/nomethoderror-in-book-show-subjects/156260>\
**Category:** Rails\
**Created:** [December 23, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-book-show-subjects/156260 "2008-12-23T16:07:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@manisha](https://www.ruby-forum.com/u/manisha)\
**Post date:** [December 23, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-book-show-subjects/156260/1 "2008-12-23T16:07:06Z")

</div>

Hi,

I am quite new to ROR.I am trying to create a rails simple application  
that has informations about Books and Subjects.Srangely, I finished  
creating a basic application, and it worked fine.  
But then , I just tried to add some more values in the database, and  
faced problem with migration, that just made me crazy. SO I deleted the  
database and did the database and migration thing all over again without  
touching rest of the codes.Now the application runs, but when I click on  
the subject name, it show error, although it was working well before.Can  
someone help me please in this regards ???

The ERROR is :

NoMethodError in Book#show\_subjects

Showing book/show\_subjects.rhtml where line #1 raised:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.name

Extracted source (around line #1):

1:

# \<%= @subject.name -%\>
  
2: 
  
3: \<% @subject.books.each do |c| %\>  
4: - \<%= link\_to c.title, :action =\> “show”, :id =\> c.id -%\>

I have two tables in my database : books and subjects

The Controller is

class BookController \< ApplicationController  
layout ‘standard’  
def list

```
@books = Book.find(:all)

```

end

def show

```
@book = Book.find(params[:id])

```

end

def new  
@book = Book.new  
@subjects = Subject.find(:all)  
end

def create  
@book = Book.new(params[:book])  
if @book.save  
redirect\_to :action =\> ‘list’  
else  
@subjects = Subject.find(:all, :order =\> “name”)  
render :action =\> ‘new’  
end  
end

def edit

```
@book = Book.find(params[:id])
@subjects = Subject.find(:all)

```

end

def update  
@book = Book.find(params[:id])  
if @book.update\_attributes(params[:book])  
redirect\_to :action =\> ‘show’, :id =\> @book  
else  
@subjects = Subject.find(:all)  
render :action =\> ‘edit’  
end  
end

def delete  
Book.find(params[:id]).destroy  
redirect\_to :action =\> ‘list’  
end

def show\_subjects  
@subjects = Subject.find(params[:id])

end

end

The show\_subjects.rhtml file is :

# \<%= @subject.name -%\>

\<% @subject.books.each do |c| %\>- \<%= link\_to c.title, :action =\> "show", :id =\> c.id -%\>
\<% end %\>

---

<div class="post-metadata">

**Author:** ![manisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@manisha](https://www.ruby-forum.com/u/manisha)\
**Post date:** [December 23, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-book-show-subjects/156260/2 "2008-12-23T16:16:44Z")

</div>

> end

I don’t know if this is a typo in your post or not, but your  
#show\_subjects  
method sets the “@subjects” (note the plural) instance variable whereas  
your  
view references the “@subject” (note – singular) instance variable.

–wpd

---

<div class="post-metadata">

**Author:** ![manisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@manisha](https://www.ruby-forum.com/u/manisha)\
**Post date:** [December 23, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-book-show-subjects/156260/3 "2008-12-23T16:25:13Z")

</div>

Thanks a lot Patrick, it works now!!!  
thanks a lot.

Patrick D. wrote:

> > end
> 
> I don’t know if this is a typo in your post or not, but your  
> #show\_subjects  
> method sets the “@subjects” (note the plural) instance variable whereas  
> your  
> view references the “@subject” (note – singular) instance variable.
> 
> –wpd
