我有以下代码:
def first
@distribution_sheet = DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","closed"]).last
@results = @distribution_sheet.results
@specimens = @distribution_sheet.specimens
end
include DisplayResultHelper
def show
respond_to do |format|
format.html
format.csv do
@results.first.each do |res|
send_data(
generate_csv([
["Lab No","Assay","Batch","Cuttoff"],
[res.lab_id, res.kit, res.batch_number, res.cutoff]
]),
:filename => "my_data-#{Time.now.to_date.to_s}.csv",
:type => 'text/csv'
)
如上所述调用方法first
是不是正确的:@results.first.each do |res|...
?这给出了错误消息:You have a nil object when you didn't expect it!
此代码的目的是将结果导出为csv格式。
答案 0 :(得分:1)
@results
或first
为nil
。请检查以确保它们已填充。