# Elusive Nil Object error on Comments

**URL:** <https://www.ruby-forum.com/t/elusive-nil-object-error-on-comments/115140>\
**Category:** Rails\
**Created:** [September 20, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/elusive-nil-object-error-on-comments/115140 "2007-09-20T23:47:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [September 20, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/elusive-nil-object-error-on-comments/115140/1 "2007-09-20T23:47:57Z")

</div>

I have 2 models: Applicants and Comments. Applicants has\_many  
Comments and Comments belongs\_to Applicant.

If I add validation to my Applicants model (for example,  
“validates\_presence\_of :first\_name”) and edit the applicants page, I  
get a Nil Object error if I remove the first\_name before saving. In  
this case I would expect to see the “there was 1 error that  
prevented…” error message. I do get this message during Applicant  
creation.

The strange part is that the error I do get points to my comments  
display. Everything works as expected. Why would it behave this way?

##### ERROR MESSAGE: Action Controller: Exception Caught

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

Showing app/views/applicants/\_comments.rhtml where line #42 raised:

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

Extracted source (around line #42):

39:

Comment  
40: File  
41:   
42: \<% for comment in @comments %\>  
43:   
44: \<%= comment.created\_at.to\_s(:long) %\>   
45: \<%=h comment.comment\_type %\> 
#### CONTROLLER: applicants\_controller.rb

def edit  
@applicant = Applicant.find(params[:id])  
@comments = @applicant.applicant\_comments.find(:all)  
end

def update  
@applicant = Applicant.find(params[:id])  
if @applicant.update\_attributes(params[:applicant])  
flash[:notice] = ‘Applicant was successfully updated.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘edit’  
end  
end

def comment

Applicant.find(params[:id]).applicant\_comments.create(params[:applicant\_comment])  
flash[:notice] = ‘comment was successfully created.’  
redirect\_to :action =\> ‘edit’, :id =\> params[:id]  
end

#### MODEL: ApplicantComment.rb

class ApplicantComment \< ActiveRecord::Base  
belongs\_to :applicant

has\_attachment :storage =\> :file\_system  
end

#### MODEL: Applicant.rb

class Applicant \< ActiveRecord::Base  
has\_one :contract  
has\_many :applicant\_comments

validates\_presence\_of :first\_name, :last\_name, :email, :phone\_1,  
:address\_1, :city, :state, :zip, :resume

def full\_name  
[last\_name, first\_name].join(’, ‘)  
end  
def full\_name=(name)  
split = name.split(’ ', 2)  
self.first\_name = split.last  
self.last\_name = split.first  
end  
end

#### VIEW: applicants/edit.rhtml

# Edit applicant

\<% form\_tag :action =\> ‘update’, :id =\> @applicant do %\>  
\<%= render :partial =\> ‘form’ %\>

 \<%= image\_submit\_tag("save.png") %\> 
\<% end %\>
# Post Comments
 \<% for comment in @comments %\> \<% end %\>

| Comment | Created |
| --- | --- |
| \<%= comment.body %\> | \<%= comment.created\_at %\> |

\<% form\_for(  
:applicant\_comment,  
:url =\> {:controller =\> ‘applicants’ , :action =\> ‘comment’, :id =\>  
@applicant},  
:html =\> { :multipart =\> true }  
) do |form| %\>

\<%= form.hidden\_field(“comment\_type”, :value =\> “Hiring Process”) %\>

Comment:  
 \<%= form.text\_area :body, :rows =\> 3, :cols =\> 40 %\>

 \<%= link\_to\_function(("Attach File"), "Element.remove('file\_link');Element.show('file')") %\> 

 Upload a file:  
 \<%= form.file\_field :uploaded\_data %\> 

 \<%= image\_submit\_tag("save.png") %\> 
\<% end %\>

\<%= link\_to ‘Back’, :action =\> ‘list’ %\>

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [September 21, 2007, 1:03am UTC](https://www.ruby-forum.com/t/elusive-nil-object-error-on-comments/115140/2 "2007-09-21T01:03:04Z")

</div>

ebrad wrote:

> ```
> flash[:notice] = 'Applicant was successfully updated.'
> redirect_to :action => 'list'
> else
> render :action => 'edit'
> end
> 
> ```
> 
> end

“render :action =\> ‘edit’” renders the edit template but does  
not call the edit action which is seting the @comments variable.

Since you want to preserve the errors on @applicant, you’ll  
have to copy down the @comments line.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [September 21, 2007, 3:08am UTC](https://www.ruby-forum.com/t/elusive-nil-object-error-on-comments/115140/3 "2007-09-21T03:08:16Z")

</div>

That’s exactly it! Thanks!

#### CONTROLLER: applicants\_controller.rb

def edit  
@applicant = Applicant.find(params[:id])  
@comments = @applicant.applicant\_comments.find(:all)  
end

def update  
@applicant = Applicant.find(params[:id])  
if @applicant.update\_attributes(params[:applicant])  
flash[:notice] = ‘Applicant was successfully updated.’  
redirect\_to :action =\> ‘list’  
else  
@comments = @applicant.applicant\_comments.find(:all)  
render :action =\> ‘edit’  
end  
end
