Help with 'error_messages_for'


#1

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


#2

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


#3

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.


#4

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


#5

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


#6

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


#7

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


#8

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