# Problems with nil object

**URL:** <https://www.ruby-forum.com/t/problems-with-nil-object/134718>\
**Category:** Rails\
**Created:** [April 14, 2008, 1:20pm UTC](https://www.ruby-forum.com/t/problems-with-nil-object/134718 "2008-04-14T13:20:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mckracken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mckracken](https://www.ruby-forum.com/u/mckracken)\
**Post date:** [April 14, 2008, 1:20pm UTC](https://www.ruby-forum.com/t/problems-with-nil-object/134718/1 "2008-04-14T13:20:15Z")

</div>

Hi to all. I’ve a very big problem. I know this can seem very loing, but  
please, try to read it. I have a simply blog, with posts and their  
comments. I’ve two tables in my db, posts, with post id, text and  
comments\_count to store the number of comments-children of each post (as  
recomanded to do in AWDWR); and the table comments, with comment id,  
post\_id for the post they’re referred to, and text (there are many other  
fields, but they’re not importat here). I have a home\_controller, this  
is the code:

class HomeController \< ApplicationController

```
def index
  @posts = Post.find(:all, :order => "data DESC")
end

def show_comment
  @posts = Post.find(:all, :order => "data DESC")
  @comments = Comment.find(:all, :conditions => ['post_id= ?',

```

params[:id] ])  
end

```
def add_comment
  @posts = Post.find(:all, :order => "data DESC")
  @comments = Comment.find(:all, :conditions => ['post_id= ?',

```

params[:id] ])  
@com = Comment.new(:post\_id =\> params[:post\_id])  
if flash[:comment]  
@comment = flash[:comment]  
else  
@comment = Comment.new  
end  
end

```
def save_comment
  if request.post?
    @comment = Comment.new(params[:comment])
    if @comment.save
      flash[:notice] = "Commento inserito con successo"
      redirect_to("/home/show_comment/" +

```

params[:comment][:post\_id])  
else  
flash[:comment] = @comment  
redirect\_to("/home/add\_comment/" + params[:comment][:post\_id])  
end  
end  
end

end

and three views, index, add\_comment and show\_comment.

The problem arise with add\_comment. When i want to add a comment for a  
post without any comment (i’m the first to insert a comment for a post),  
it displays:

> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.post\_id
> 
> Extracted source (around line #17):
> 
> 14:
> 
>   
> 15: \<% end %\>  
> 16: \<% end %\>  
> 17: \<%= com.post\_id %\>  
> 18: \<% if @com.post\_id == post.id %\>  
> 19: \<%= error\_messages\_for :comment %\>  
> 20: \<% form\_for @comment, :url =\> {:action =\> :save\_comment} do |form| %\>

This is the code for the add\_comment page

```
 <% for post in @posts %>
  <div class="post">
      <p>Titolo: <%= post.titolo %> <span>data: <%= post.data

```

%\>

  

Testo:   
\<%= post.testo %\>

  
\<%= link\_to ‘Commenti(’ + post.comments.size.to\_s + ‘)’,  
:action =\> ‘show\_comment’, :id =\> post %\>  
\<%= link\_to ‘Aggiungi un commento’, :action =\>  
‘add\_comment’, :id =\> post %\>  

  
\<% for com in @comments %\>  
\<% if com.post\_id == post.id %\>  

\<%= com.testo %\>

  
\<% end %\>  
\<% end %\>  
\<% if com.post\_id == post.id %\>  
\<%= error\_messages\_for :comment %\>  
\<% form\_for @comment, :url =\> {:action =\>  
:save\_comment} do |form| %\>  
  

Autore:  
  
\<%= form.text\_field :autore%\>

  

Testo:  
\<%= form.text\_area :testo %\>

  
\<%= form.hidden\_field :post\_id, :value =\>  
post.id %\>  
\<%= submit\_tag “Invia commento” , :class =\>  
“submit” %\>  
\<% end %\>  
\<%end%\>  

```
  </div> <!-- post -->
  <% end %>

```

PLEASE HELP ME to solve this problem!

---

<div class="post-metadata">

**Author:** ![mckracken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mckracken](https://www.ruby-forum.com/u/mckracken)\
**Post date:** [April 14, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/problems-with-nil-object/134718/2 "2008-04-14T13:47:02Z")

</div>

On 14 Apr 2008, at 12:20, Emanuele B. wrote:

> fields, but they’re not importat here). I have a home\_controller, this  
> \<% if com.post\_id == post.id %\>  
> com isn’t defined here. What are you expecting it to be

Fred

---

<div class="post-metadata">

**Author:** ![mckracken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mckracken](https://www.ruby-forum.com/u/mckracken)\
**Post date:** [April 14, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/problems-with-nil-object/134718/3 "2008-04-14T16:00:54Z")

</div>

Line 16 closes the “for com in @comments” loop so “com” goes out of  
scope. That’s why you are getting the nil error. I think you want to  
move that \<% end %\> further down. Think through what the loop should  
be doing and make sure it wraps all that code.

🙂

-Danimal
