0.0
No commit activity in last 3 years
No release in over 3 years
Adds a render_csv to your Rails 3.0+ controllers
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.13.1

Runtime

>= 3.0.0
 Project Readme

RenderedCsv

RenderedCsv provides a render_csv method to your Rails 3.0+ controllers.

Installation

Add this line to your application's Gemfile:

gem 'rendered_csv'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rendered_csv

Usage

In your controllers, simply call render_csv instead of render. This will setup the appropriate headers and render your CSV file.

render_csv has two optional parameters, filename and template. If not specified, filename defaults to the name of the current action.

This example would respond with a file named widgets.csv, rendering the index template in the process:

class WidgetsController < ApplicationController
  def index
    @widgets = Widget.all
    respond_to do |format|
      format.csv do
        render_csv('widgets')
      end
    end
  end
end

You could render a different template: render_csv('widgets', 'other_index')

Or you could just use the defaults to get index.csv from the index template: render_csv

Acknowledgements

The meat of this gem was taken from Clinton R. Nixon's answer to this StackOverflow question. I just wanted to stop copy-pasting the code from project to project.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request