# Help with 'error\_messages\_for'

**URL:** <https://www.ruby-forum.com/t/help-with-error-messages-for/63285>\
**Category:** Rails\
**Created:** [June 13, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285 "2006-06-13T21:28:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [June 13, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285/1 "2006-06-13T21:28:01Z")

</div>

Greetings,

I’m in the throws of deploying an app on site5, and I’m getting strange  
errors. I think I might need to understand how error\_messages\_for  
works.

I get a pretty standard looking error:

"  
You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occured while evaluating nil.errors

Extracted source (around line #7):

4:  
5:

  
6: \<% @title = ‘Upload New Image’ %\>  
7: \<%= error\_messages\_for(‘image’) %\>  
"

the production box is apache fastcgi linux, my dev box is windows  
webrick. This seems odd to me because I’ve never had to instantiate a  
class, or check for it in order to use the error\_messages\_for method in  
my pages. Is it a versioning thing maybe?

Jason

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [June 13, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285/2 "2006-06-13T22:20:21Z")

</div>

have you tried using the symbol :image instead of “image”?

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [June 13, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285/3 "2006-06-13T22:26:40Z")

</div>

Jason P. wrote:

> You have a nil object when you didn’t expect it!  
> You might have expected an instance of ActiveRecord::Base.  
> The error occured while evaluating nil.errors
> 
> Extracted source (around line #7):
> 
> 4:  
> 5:
> 
>   
> 6: \<% @title = ‘Upload New Image’ %\>  
> 7: \<%= error\_messages\_for(‘image’) %\>
> 
> It looks like the variable @image doesn’t exist by the time the view is  
> rendered. ‘image’ in your example is supposed to be the name of an  
> instance variable. Make sure you assign an object @image in your  
> controller.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [June 13, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285/4 "2006-06-13T23:19:34Z")

</div>

if you pass a nil object to error\_messages\_for, the method should  
simply return “”

Here’s the definition:

def error\_messages\_for(object\_name, options = {})  
options = options.symbolize\_keys  
object = instance\_variable\_get("@#{object\_name}")  
if object && !object.errors.empty?  
content\_tag(“div”,  
content\_tag(  
options[:header\_tag] || “h2”,  
“#{pluralize(object.errors.count, “error”)} prohibited  
this #{object\_name.to\_s.gsub(”\_", " “)} from being saved”  
) +  
content\_tag(“p”, “There were problems with the following  
fields:”) +  
content\_tag(“ul”, object.errors.full\_messages.collect {  
|msg| content\_tag(“li”, msg) }),  
“id” =\> options[:id] || “errorExplanation”, “class” =\>  
options[:class] || “errorExplanation”  
)  
else  
“”  
end  
end

if you try “helper.error\_message\_for(nil)” you should get back “”. give  
it a try

Mike

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [June 13, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285/5 "2006-06-13T23:04:35Z")

</div>

> It looks like the variable @image doesn’t exist by the time the view is  
> rendered. ‘image’ in your example is supposed to be the name of an  
> instance variable. Make sure you assign an object @image in your  
> controller.

symbol :image doesn’t work.

isn’t error\_messages\_for used primarily for validation errors on your  
models? I thought there was implicit mapping to a model in the rails  
framework - just wondering why it works perfectly on development (and on  
a different production server I tried it didn’t error either) and errors  
at site5. I think the gems might be different versions which is my  
suspicion, but does anyone know anything else?

Jason

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [June 21, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285/6 "2006-06-21T12:35:51Z")

</div>

Hi,  
I am getting the same error with ‘error\_messages\_for’. the isn’t any  
way of validating the error on the Image model. Because what I want to  
do is to validate the text\_field, which I have define the validation  
method on Image model.

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occured while evaluating nil.errors

Extracted source (around line #3):

1:

## \<%= @page\_title = “Fax Number” -%\>
  
2:  
3: \<%= error\_messages\_for(“fax”) %\>  
4: \<%= stylesheet\_link\_tag “scaffold”, “email”, :media =\> “all” %\>  
5:  
6: 

\<%= flash[:notice] %\>

Thanx  
Steve

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [June 21, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285/7 "2006-06-21T14:10:53Z")

</div>

Hi,

I did figure out what was the problem.

on your controller index just define your class, my model name is  
fax.rb

@fax = Fax.new.

here is the full code…

class FaxController \< ApplicationController

```
   def save_faxno

     @fax = Fax.new(params[:faxes])
         if @fax.save

          flash[:notice] = 'Fax number was successful Inserted.'
          redirect_to :action => 'index'
          return
       else
          render(:action => 'fax')
        return
     end
   end

def index
   @fax = Fax.new
   render :action => 'fax'
end

```

end

> Thanx  
> Steve

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [June 29, 2006, 9:11am UTC](https://www.ruby-forum.com/t/help-with-error-messages-for/63285/8 "2006-06-29T09:11:00Z")

</div>

Mike G. wrote:

> if you pass a nil object to error\_messages\_for, the method should  
> simply return “”  
> Here’s the definition:  
> …

Thanks, this was a good hint for me.

I had a similar problem as Jason. My application had worked for quite  
some time and then all of a sudden the “error occured while evaluating  
nil.errors”.

Well, it was not all of a sudden, after all. The problem was that I  
installed the Globalize plugin which shadows the original Rails Active  
Record Helper to provide localized error messages. The shadowing code  
does not include test for the object==nil case. When you add it, the  
above error is gone and my application behaves as before (even better -  
getting globalized ;-).

# For details, see diff: Index: active\_record\_helper.rb

— active\_record\_helper.rb (Globalize for\_1.1)  
+++ active\_record\_helper.rb (working copy)  
@@ -16,7 +16,7 @@  
def error\_messages\_for(object\_name, options = {})  
options = options.symbolize\_keys  
object = instance\_variable\_get("@#{object\_name}")

- 

```
   unless object.errors.empty?

```

- 

```
   if object && !object.errors.empty?
     content_tag("div",
       content_tag(
         options[:header_tag] || "h2",

```

@@ -26,6 +26,8 @@  
content\_tag(“ul”, object.errors.full\_messages.collect {  
|msg| content\_tag(“li”, msg) }),  
“id” =\> options[:id] || “errorExplanation”, “class” =\>  
options[:class] || “errorExplanation”  
)

- 

```
   else

```

- 

```
  	""
   end
 end

```

end
