我试图在 Rails 2.3.5 中为我的一个模型创建自定义验证,但每次运行测试套件时我都会收到以下错误:
`method_missing_without_paginate': undefined local variable or method `validates_progression'
应用程序/模型/project.rb
class Project < ActiveRecord::Base
...
validates_progression
def validates_progression
true # stubtastic!
end
end
我好像不太懂这个~
最佳答案
它不起作用,因为您正在定义一个具有实例范围的方法,并且您正试图在类范围内调用它。您有两种选择:
实例范围
class Project < ActiveRecord::Base
validate :validates_progression
def validates_progression
true # stub
end
end
类范围
class Project < ActiveRecord::Base
def self.validates_progression
true # stub
end
# Be sure to define this method before calling it
validates_progression
end
除非您想包装其他过滤器,否则第二种选择实际上没有意义。
class Project < ActiveRecord::Base
def self.validates_progression
validates_presence_of :progression
validates_length_of ...
end
# Be sure to define this method before calling it
validates_progression
end
否则,使用第一个解决方案。
关于ruby-on-rails - 分页为自定义验证抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3825415/