Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

CSV renderer for Rails 3

branch: master


Rails 3 CSV renderer for ActiveRecord collections

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 }

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
Sebastian’s House 123 Pup St Burlington VT 05401 2011-07-26 03:30:22 UTC 2011-07-26 03:31:04 UTC
Someone Else 999 Herp Derp Burlington VT 05401 2011-07-27 01:35:42 UTC 2011-07-27 01:35:42 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] }

Limit columns

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

Add methods as columns

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

Add methods as columns and exclude columns

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

Contributing to render_csv

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


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

Something went wrong with that request. Please try again.