我正在尝试使用Active Record和CSV类导出数据,但我没有生成的文件,可以帮助我查看代码吗。
当我点击导出时,没有打开对话框以从CSV下载文件,我漏掉了什么吗?
我的控制器
def export_csv
@contact = Contacts.find_by_sql("select * from contacts_list limit 10")
respond_to do |format|
format.html
format.csv { send_data @contact.as_csv }
end
end
我的模型
def self.to_csv
attributes = %w{id name mail}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |contact|
csv << attributes.map{ |attr| contact.send(attr) }
end
end
end
我的看法
<%= link_to( 'Export CSV' ,{ :controller => :company, :action => :export_csv, format: "csv"}, { :class => "btn-ex" } ) %>
控制台输出
Started GET "/company/export_cs" for 127.0.0.1 at 2019-12-17 11:31:26 -0200
Processing by CompanyController#export_cs as HTML
只有这个,没有更多,没有错误,没有警告信息。