ruby-on-railsrubyredminepython-redmine

how i can fix this error? AbstractController::DoubleRenderError


I want to recover data in two different tables and put the data on excel but I have a little bug if someone has an idea. Thank you

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'

model..

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

view

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

model (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

model (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


Solution

  • You can't respond twice one request. An option would be:

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

    With this, you'll respond with one file containing attachments and projects.