调用ruby方法也导出csv

时间:2011-04-18 23:36:29

标签: ruby-on-rails

我有以下代码:

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格式。

1 个答案:

答案 0 :(得分:1)

@resultsfirstnil。请检查以确保它们已填充。