Project

render_csv

0.05
No commit activity in last 3 years
No release in over 3 years
Adds a custom CSV renderer to Rails applications
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.6
>= 1.3

Runtime

>= 3.0
 Project Readme

render_csv

Build Status Gem Version Code Climate Dependency Status

Rails CSV renderer for ActiveRecord collections

Project Status - May 2015

I am no longer maintaining this project. I have not been able to get it working predictably in newer versions of Rails or in production environments. If you want to add CSV rendering support to your Rails application, I do not recommend installing this gem.

Rails & Ruby Versions Supported

Rails: 3.0.x - 4.0.x

Ruby: 1.9.3, 2.0.0 and 2.1.0

Installation

The gem is hosted at rubygems.org

What is it?

The CSV renderer allows you to render any collection as CSV data.

class LocationsController < ApplicationController
  def index
    @locations = Location.all

    respond_to do |format|
      format.csv  { render csv: @locations }
    end
  end
end

Will render a CSV file similar to:

name address city state zip created_at updated_at
Pete's House 555 House Ln Burlington VT 05401 2011-07-26 03:12:44 UTC 2011-07-26 03:12:44 UTC
Sebastians's House 123 Pup St Burlington VT 05401 2011-07-26 03:30:44 UTC 2011-07-26 03:30:44 UTC
Someone Else 999 Herp Derp Burlington VT 05401 2011-07-26 03:30:44 UTC 2011-07-26 03:30:44 UTC

Usage Options

There are a few options you can use to customize which columns are included in the CSV file

Exclude columns

respond_to do |format|
  format.csv  { render csv: @locations, except: [:id] }
end

Limit columns

respond_to do |format|
  format.csv  { render csv: @locations, only: [:address, :zip] }
end

Add methods as columns

respond_to do |format|
  format.csv  { render csv: @locations, add_methods: [:method1, :method2] }
end

Add methods as columns and exclude columns

respond_to do |format|
  format.csv  { render csv: @locations, except: [:id], add_methods: [:method1, :method2] }
end

Copyright

Copyright © 2011-2014 Peter Brown. See LICENSE.txt for further details.