本文介绍了'对'的ActionModel未定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

NoMethodError in Users#new

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

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

在code在第7行是:

The code in line 7 is:

7:   = errors_for user, :first_name

而application_helper.rb:

And the application_helper.rb:

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

开是的ActiveRecord 一个默认方法。为什么没有这项工作?

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

推荐答案

如果你使用的Rails 3,那么问题是,有上方法的错误类没有了。我想,你应该使用获取了。所以:

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)

或者

error = case errors = model.errors[field]

这篇关于'对'的ActionModel未定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!