我尝试将生成的CSV下载到浏览器,但我无法在浏览器中下载文件。
以下是我的尝试:
def index
@campaigns = Campaign.all
respond_to do |format|
format.csv { send_data @campaigns.to_csv, filename: "campaigns-#{Date.today}.csv", disposition: "attachment" }
end
end
def self.to_csv
attributes = %w(title)
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |campaign|
csv << campaign.attributes.values_at(*attributes)
end
end
end
当我点击此控制器操作时,它似乎成功创建了数据的CSV,但它并未在浏览器中下载。我在这做错了什么?谢谢!