当我从我的模型中调用某个方法时,我不断收到未定义的方法。
class User < ActiveRecord::Base
def update!
request_info
end
def request_info
return "hmmm"
end
end
更新中的request_info!没有定义 我也试过让它成为 self.request_info 但这也不起作用
最佳答案
在rails 中有两种调用方法的方法。
class Foo
def self.bar
puts 'class method'
end
def baz
puts 'instance method'
end
end
Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class
Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>
你也是这样吗?我从 here 中获取了这个示例.查看该页面了解详细信息。
关于ruby-on-rails - 在 Rails 模型中调用方法时未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377218/