上面一篇文章将csv导入到数据库,现在将数据库的数据导出
view层
href="../../contact_manager/csv_export"使用url转向的contact_manager是action,csv_export是action中的方法
<a href="../../contact_manager/csv_export">export csv</a>

控制层
class InformationController < ApplicationController
 require 'faster_csv'
 
 def csv_export
   @informations = CustomerInformation.find(:all)
   
   csv_string = FasterCSV.generate do |csv|
     csv << ["name","email","remark"]
     @informations.each do |u|
       csv << [u.name,u.email,u.remark]
     end
   end
   send_data csv_string,
             :type=>'text/csv; charset=iso-8859-1; header=present',
             :disposition => "attachment; filename=export.csv"
 end
  
end

实体
class CustomerInformation < ActiveRecord::Base
end
评论
发表评论

您还没有登录,请登录后发表评论

kukuqiu001
搜索本博客
博客分类
存档
最新评论