Undefined method 'on' for ActionModel

北慕城南 提交于 2019-11-30 03:17:49

问题


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?


回答1:


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]



回答2:


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...



来源:https://stackoverflow.com/questions/7526499/undefined-method-on-for-actionmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!