I''m trying to create custom error messages and in doing so I thought I could access the validation errors each_key method. I know each_key isn''t listed as a method for errors object but errors object is a hash right? When I look at the source of each method def each @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } end each_key is being used but when I access it directly, it barfed with undefined method error. Anyone knows why? Thanks in advance for your help. -- Posted via http://www.ruby-forum.com/.
@errors is an instance variable on the model - I doubt you''re accessing that from the controller... I''m also not clear what''s wrong with @model.errors.each - it gets you exactly the same results as iterating with each_key on the underlying array... --Matt Jones On Aug 12, 8:04 pm, Cali Wildman <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m trying to create custom error messages and in doing so I thought I > could access the validation errors each_key method. I know each_key > isn''t listed as a method for errors object but errors object is a hash > right? When I look at the source of each method > > def each > @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg }} > > end > > each_key is being used but when I access it directly, it barfed with > undefined method error. Anyone knows why? Thanks in advance for your > help. > -- > Posted viahttp://www.ruby-forum.com/.
Matt Jones wrote:> @errors is an instance variable on the model - I doubt you''re > accessing that from the controller... > > I''m also not clear what''s wrong with @model.errors.each - it gets you > exactly the same results as iterating with each_key on the underlying > array... > > --Matt Jones > > > On Aug 12, 8:04�pm, Cali Wildman <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt>There isn''t anything wrong with @model.errors.each, I was experimenting with different ways to get at the unique keys since there may be more than 1 error message per key (field). I ended up creating a local hash variable to get the functionality I want. Thanks for your insight. -- Posted via http://www.ruby-forum.com/.