9

I'm getting the following error:

NoMethodError in Users#new

Showing .../app/views/users/form/_new.haml where line #7 raised:

undefined method `on' for #<ActiveModel::Errors:0x007fb599ec6610>

The code in line 7 is:

7:   = errors_for user, :first_name

And the application_helper.rb:

def errors_for(model, field)
  error = case errors = model.errors.on(field)
  ...
end

'on' is a default method in ActiveRecord. Why doesn't this work?

Patrick
  • 7,903
  • 11
  • 52
  • 87

2 Answers2

15

If you're using Rails 3, then the problem is that there's no "on" method for the Errors class anymore. I think you're supposed to use "get" now. So:

error = case errors = model.errors.get(field)

Or...

error = case errors = model.errors[field]
  • 1
    Basically "on" used to be an alias for [], and they changed it to "get". I'd just use [] (which would work in both Rails 2 and 3). – Chadwick Wood Sep 27 '11 at 16:50
1

I checked my user and u.errors is an ActiveRecord::Errors, while I see you have an ActiveModel::Error, I would work on that.

Then I don't understand case errors = statement in your helper, I'm curious to know how you implemented that part...

ecoologic
  • 10,202
  • 3
  • 62
  • 66
  • I was wondering about that, too. But when I run User.new in the rails console, everything works fine. The code is from Teambox (https://github.com/teambox/teambox/blob/dev/app/helpers/application_helper.rb), trying to run it locally. – Patrick Sep 23 '11 at 12:10
  • the case statement makes more sense now (though I think if you have 2 errors on the same field you loose the second?). – ecoologic Sep 23 '11 at 15:15
  • Well, I'm just trying to get the code running, I didn't write it. ;) I removed the whole statement, but it still returns with the same error. No clue what's wrong there. Especially that it checks on the model and not ActiveRecords. – Patrick Sep 23 '11 at 15:43
  • I tried to install the repo but I had some problems with the gem versions so I gave up, did you get it straight running? – ecoologic Sep 25 '11 at 11:57
  • Yes, took me quite some time to get all the right gem versions together. Here's my fork: https://github.com/PatrickHeneise/teambox – Patrick Sep 25 '11 at 14:13
  • still I can't bundle, I'm not going to spend time to find the right configuration (though it's weird), can you debug and check the instance of `@user`, is it an `ActiveRecord`? - and lastly, can you ask somebody in the project to answer your question? – ecoologic Sep 26 '11 at 22:36
  • sorry I couldn't do any better, if you find the problem you can answer you own question and mark it as solved – ecoologic Sep 27 '11 at 08:33
  • Well, I kind of gave up for the moment as well. Will wait for the next version on GitHub to see if it works better. – Patrick Sep 27 '11 at 08:39