Paginate 会引发自定义验证错误
我正在尝试在 Rails 2.3.5 中为我的模型之一创建自定义验证,但每次运行测试套件时我都会收到以下错误:
`method_missing_without_paginate': undefined local variable or method `validates_progression'
app/models/project.rb
class Project < ActiveRecord::Base
...
validates_progression
def validates_progression
true # stubtastic!
end
end
我似乎无法充分利用这个~
I'm attempting to create a custom validation for one of my models in Rails 2.3.5, but I keep recieving the following error everytime I run my testing suite:
`method_missing_without_paginate': undefined local variable or method `validates_progression'
app/models/project.rb
class Project < ActiveRecord::Base
...
validates_progression
def validates_progression
true # stubtastic!
end
end
I can't seem to make much of this~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不起作用,因为您正在定义具有实例范围的方法,并且尝试在类范围内调用它。您有两种选择:
实例范围
类范围
除非您想包装其他过滤器,否则第二种选择实际上没有意义。
否则,请采用第一个解决方案。
It doesn't work because you are defining a method with instance scope and you are trying to call it within the class scope. You have two alternatives:
Instance Scope
Class scope
The second alternative doesn't really makes sense unless you want to wrap an other filter.
Otherwise, go with the first solution.
分页参考是一个转移注意力的事情。线索就是“没有”。 will paginate gem 已为现有
method_missing
方法起了别名,并将其命名为method_missing_without_pagination
。因此,问题是标准缺失方法错误。该方法缺失,因为 a) 在调用它时未定义它,b) 不在正确的范围内(您试图在类的范围内调用实例方法)。
您可以使用
validate
以及验证方法的符号来添加自定义验证:The pagination reference is a red herring. The clue is the 'without'. The will paginate gem has aliased the existing
method_missing
method and called itmethod_missing_without_pagination
. So, the problem is a standard missing method error.The method is missing because it is a) not defined when you call it and b) not in the correct scope (you are trying to call an instance method in the scope of the class).
You can add your custom validation by using
validate
with the symbol for your validation method: