Paginate 会引发自定义验证错误

发布于 2024-09-25 14:55:33 字数 396 浏览 7 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-10-02 14:55:33

它不起作用,因为您正在定义具有实例范围的方法,并且尝试在类范围内调用它。您有两种选择:

实例范围

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

否则,请采用第一个解决方案。

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 Project < ActiveRecord::Base

  validate :validates_progression

  def validates_progression
     true # stub
  end

end

Class scope

class Project < ActiveRecord::Base

  def self.validates_progression
     true # stub
  end

  # Be sure to define this method before calling  it
  validates_progression

end

The second alternative doesn't really makes sense unless you want to wrap an other filter.

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

Otherwise, go with the first solution.

心碎无痕… 2024-10-02 14:55:33

分页参考是一个转移注意力的事情。线索就是“没有”。 will paginate gem 已为现有 method_missing 方法起了别名,并将其命名为 method_missing_without_pagination。因此,问题是标准缺失方法错误。

该方法缺失,因为 a) 在调用它时未定义它,b) 不在正确的范围内(您试图在类的范围内调用实例方法)。

您可以使用 validate 以及验证方法的符号来添加自定义验证:

validate :validates_progression

def validates_progression
  true
end

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 it method_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:

validate :validates_progression

def validates_progression
  true
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文