我正在使用will_paginate gem,并试图按照这个RailsCast实现DataTables,并发现提供属性total_entries是个好主意,因为will_paginate显然不能很好地完成这个。
所以我把这个方法添加到我的班级中:
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
但是,当视图尝试加载时,我仍然会收到此错误(这是网络服务器响应的屏幕截图):
(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'
显然我在定义方法时必须做错事;但什么?我在Rails中缺乏经验,无法摆脱这个......
提前致谢,
瑞克