Forum: Ruby on Rails RE: possible rails bug? form errors and scoping

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.
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 jviney (Guest)
on 2005-12-09 05:38
(Received via mailing list)
I think you've run into the same thing I did. error_messages_for takes a
string as its first parameter and then looks for an instance variable by
that name.

def error_messages_for(object_name, options = {})
  options = options.symbolize_keys
  object = instance_variable_get("@#{object_name}")
  unless object.errors.empty?
    ...
  end
end

So if you do error_messages_for('person') it will generate the errors
for @person. I personally think this should be changed, I can't see why
it takes a string in anyway... but I guess there could be a reason.

A simple fix is to just pass in the object itself, so it becomes:

def error_messages_for(object, options = {})
  options = options.symbolize_keys
  unless object.errors.empty?
    ...
  end
end

(Put the above in application_helper.rb). So you now call it like
error_messages_for person, not error_messages_for 'person'. That should
work fine.

Cheers, Jonathan.
This topic is locked and can not be replied to.