我该如何解决这个错误? AbstractController::DoubleRenderError

how i can fix this error? AbstractController::DoubleRenderError

我想恢复两个不同表中的数据并将数据放在 excel 但如果有人有想法,我会遇到一个小错误。谢谢

class ExportController < ApplicationController

def index
@attachments= Attachment.all
@projects= Project.all
 respond_to do |format|
  format.html
  format.csv {send_data @attachments.to_csv}
  format.xls {send_data @attachments.to_csv(col_sep: "\t") and send_data @projects.to_csv(col_sep: "\t")}

end

end 
end

route.rb

get '/export', to: 'export#index'

型号..

class Export < ActiveRecord::Base

   def self.to_csv(options = {})
      CSV.generate(options) do |csv|
      csv << column_names
      all.each do |table|
      csv << table.attributes.values_at(*column_names)
      end
    end
  end

  end

查看

<h1>Exportation</h1>
<%= link_to 'Download as .xlsx', export_path(format: :xls) %>

型号(project.rb)

  class Project < ActiveRecord::Base
    def self.to_csv(options = {})
    CSV.generate(options) do |csv|
    csv << column_names
    all.each do |project|
   csv << project.attributes.values_at(*column_names)
  end
  end
 end
end

型号 (attachment.rb)

class Attachment < ActiveRecord::Base
 def self.to_csv(options = {})
      CSV.generate(options) do |csv|
      csv << column_names
      all.each do |table|
      csv << table.attributes.values_at(*column_names)
      end
    end
  end
  end

error image

您不能对一个请求回复两次。一个选项是:

format.xls {send_data @attachments.to_csv(col_sep: "\t") + @projects.to_csv(col_sep: "\t") }

有了这个,您将用一个包含附件和项目的文件作为回应。