Forum: Ruby on Rails help with 'error_messages_for'

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F09d113e360b156e3f4d3ddc00ccf6c3?d=identicon&s=25 Jason P. (jpfeifer)
on 2006-06-13 21:28
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: <div class="admin_content_row">
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
D0802a5ae80c2a40a9ef07967c6dc362?d=identicon&s=25 Mike G. (random52k)
on 2006-06-13 22:20
(Received via mailing list)
have you tried using the symbol :image instead of "image"?
Alex Wayne (Guest)
on 2006-06-13 22:26
Jason Pfeifer 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: <div class="admin_content_row">
> 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.
F09d113e360b156e3f4d3ddc00ccf6c3?d=identicon&s=25 Jason P. (jpfeifer)
on 2006-06-13 23:04
> 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
D0802a5ae80c2a40a9ef07967c6dc362?d=identicon&s=25 Mike G. (random52k)
on 2006-06-13 23:19
(Received via mailing list)
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
Steven (Guest)
on 2006-06-21 12:35
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: <h2><%= @page_title = "Fax Number" -%></h2>
2:
3: <%= error_messages_for("fax") %>
4: <%= stylesheet_link_tag "scaffold", "email", :media => "all" %>
5:
6: <p style="color: blue"><%= flash[:notice] %>

Thanx
Steve
steven (Guest)
on 2006-06-21 14:10
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
546d21d34877b3b72aa7dbdb148a1513?d=identicon&s=25 Miroslav S. (misky)
on 2006-06-29 09:11
Mike Garey 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
This topic is locked and can not be replied to.