在 Rails 中,如何将记录作为 csv 文件返回 [英] in rails, how to return records as a csv file

查看:29
本文介绍了在 Rails 中,如何将记录作为 csv 文件返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为条目"的简单数据库表:

I have a simple database table called "Entries":

class CreateEntries < ActiveRecord::Migration
  def self.up
    create_table :entries do |t|
      t.string :firstName
      t.string :lastName
      #etc.
      t.timestamps
    end
  end

  def self.down
    drop_table :entries
  end
end

如何编写一个处理程序,将 Entries 表的内容作为 CSV 文件返回(理想情况下,它会自动在 Excel 中打开)?

How do I write a handler that will return the contents of the Entries table as a CSV file (ideally in a way that it will automatically open in Excel)?

class EntriesController < ApplicationController

  def getcsv
    @entries = Entry.find( :all )

    # ??? NOW WHAT ????

  end

end

推荐答案

有一个名为 FasterCSV 的插件可以很好地处理这个问题.

There is a plugin called FasterCSV that handles this wonderfully.

这篇关于在 Rails 中,如何将记录作为 csv 文件返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆