读取错误:#<nomethoderror:undefined =“” method =“”`bytesize'=“” for =“”#<proc:0x00007f151467dba0 =“” >>

时间:2018-09-20 07:37:15

标签: ruby-on-rails ruby

您好,我正在使用Rails传输大型数据响应,以使用Rails 5下载CSV文件。我收到#>`错误Read error: #<NoMethodError: undefined method bytesize'。我的导出方法是:

def export
  file_name_prefix = ((@filter.resource.respond_to?(:csv_name)) ? @filter.resource.csv_name : @filter.resource.name.pluralize)
  file_name = "#{file_name_prefix}_#{Time.now.strftime("%d_%b_%Y-%H_%M_%S")}.csv"
  headers["Content-Type"] = "text/csv; charset=utf-8; header=present"
  headers["Content-Disposition"] = "attachment; filename=\"#{file_name}\""
  erroneous_call_to_proc = false
  utf8_byte_order_mark = "\xEF\xBB\xBF"
  self.response_body = proc { |response, output|
    unless erroneous_call_to_proc
      output.write(utf8_byte_order_mark + @filter.resource.csv_headers.join(',') + "\n")
      @filter.resource.find_each_with_order({:filtered_resource => @filter.filter_data}) { |u| output.write(u.to_s) }
    end
    erroneous_call_to_proc = true
  }
end

开始下载后,我在控制台上收到此错误。我还尝试了一种新方法,如下所示:

def export
  headers['X-Accel-Buffering'] = 'no' # Stop NGINX from buffering
  headers["Cache-Control"] = "no-cache" # Stop downstream caching
  headers["Transfer-Encoding"] = "chunked" # Chunked response header
  headers.delete("Content-Length") # See one line above

  self.response_body = Enumerator.new do |lines|
    # Uses ActiveRecord Batch API to find records efficently
    @filter.resource.find_each do |record|
      lines << record.to_csv
    end
  end
end

但是我对[10000]:Array>的数组Read error: #<NoMethodError: undefined method bytesize'遇到了相同的错误 请帮我如何解决它。预先感谢。

0 个答案:

没有答案