Ruby on rails 3 为什么这种方法不能防止命名错误?
我正在使用will_paginate gem,并尝试根据这个RailsCast实现数据表,发现提供属性total_条目是个好主意,因为will_paginate显然做得不太好 因此,我将此方法添加到我的类中,如下所示:Ruby on rails 3 为什么这种方法不能防止命名错误?,ruby-on-rails-3,datatables,will-paginate,Ruby On Rails 3,Datatables,Will Paginate,我正在使用will_paginate gem,并尝试根据这个RailsCast实现数据表,发现提供属性total_条目是个好主意,因为will_paginate显然做得不太好 因此,我将此方法添加到我的类中,如下所示: class Genotype < ActiveRecord::Base attr_accessible :allele1, :allele2, :run_date belongs_to :gmarkers belongs_to :gsamples def
class Genotype < ActiveRecord::Base
attr_accessible :allele1, :allele2, :run_date
belongs_to :gmarkers
belongs_to :gsamples
def total_entries
@t = Genotype.count;
return @t
end
end
很明显,我在定义我的方法时肯定做错了什么;但是什么呢?我在Rails方面太没经验了,所以我很难走出这个困境
提前感谢,
Rick它与方法无关,它与在nil上调用它有关。如果你能解释我应该如何定义total_条目,以便它更正错误,那将更有帮助……它与方法无关。问题是它是在
nil
对象上调用的。换句话说,您基本上说的是nil.total\u entries
,如果不调用,则没有任何total\u entries
调用是有意义的。问题不在你发布的代码中,而是在序列化中;这有点帮助。我真正的目标是根据RailsCast340提供的示例使数据表工作……但这是另一篇文章。。。
(7.6ms) SELECT COUNT(*) FROM "genotypes"
Completed 500 Internal Server Error in 96ms
NoMethodError (undefined method `total_entries' for nil:NilClass):
app/datatables/genotypes_datatable.rb:13:in `as_json'
app/controllers/genotypes_controller.rb:7:in `block (2 levels) in index'
app/controllers/genotypes_controller.rb:5:in `index'