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

时间:2018-07-06 07:42:29

标签: ruby-on-rails ruby redmine python-redmine

我想恢复两个不同表中的数据并将数据放在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

1 个答案:

答案 0 :(得分:0)

您不能一次响应两次请求。一个选择是:

    public function getTest(&$pa)
    {
        for ($i = 0; $i < 10; $i++) {
            $pa['items'][] = [
                "Var: " . $i,
                1 * $i,
            ];
        }
    }

这样,您将用一个包含附件和项目的文件来响应。