Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: How to respond_with csv without having a template file?

I have an object that has a to_csv method and I want to pass it to respond_with to render csv from my controller. My code looks like this:

class Admin::ReportsController < AdminController

  respond_to :csv

  def trips
    respond_with TripReport.new
  end
end

Instances of TripReport have a to_csv method.

When I make a request to that action I get the following error:

ActionView::MissingTemplate (Missing template admin/reports/trips with {:formats=>[:csv], :handlers=>[:erb, :builder, :rjs, :rhtml, :rxml], :locale=>[:en, :en]} in view paths

So it looks like the controller is looking for a template file to render. How can I get around this?

I'd rather the csv format responded in a similar way to json, so it calls to_csv on the object and just renders the output, is this possible?

like image 864
Oliver Nightingale Avatar asked Mar 03 '11 14:03

Oliver Nightingale


2 Answers

I've been struggling with the exact same problem. I might have found a solution.

I found some clues while reading the Renderers.add source code for :json and :xml (link is for Rails 3.0.10 code, 3.1 might have some changes already): https://github.com/rails/rails/blob/v3.0.10/actionpack/lib/action_controller/metal/renderers.rb

First, add a simple as_csv method to your model definition:

class Modelname < ActiveRecord::Base
  # ...
  def as_csv
    attributes
  end
end

This can be anything, just make sure to return a hash with key/value pairs. A Hash works better than an Array, as with keys you're able to add a header row to the CSV output later on. The idea for as_csv comes from Rails' as_json method, which return a Ruby object that is used by to_json to generate the actual JSON (text) output.

With the as_csv method in place, put the following code in a file in config/initializers inside your app (name it csv_renderer.rb, for example):

require 'csv' # adds a .to_csv method to Array instances

class Array 
  alias old_to_csv to_csv #keep reference to original to_csv method

  def to_csv(options = Hash.new)
    # override only if first element actually has as_csv method
    return old_to_csv(options) unless self.first.respond_to? :as_csv
    # use keys from first row as header columns
    out = first.as_csv.keys.to_csv(options)
    self.each { |r| out << r.as_csv.values.to_csv(options) }
    out
  end
end

ActionController::Renderers.add :csv do |csv, options|
  csv = csv.respond_to?(:to_csv) ? csv.to_csv() : csv
  self.content_type ||= Mime::CSV
  self.response_body = csv
end

And finally, add CSV support to your controller code:

class ModelnamesController < ApplicationController
  respond_to :html, :json, :csv

  def index
    @modelnames = Modelname.all
    respond_with(@modelnames)
  end

  # ...

end

The initializer code is largely based on the :json and :xml behaviour from the Rails source code (see link above).

Currently, the options hash passed to the block doesn't get passed to the to_csv call, as CSV is quite picky on which options it allows to be sent. Rails adds some default options by itself (like :template and some others), which gives you an error when passing them to to_csv. You can change the default CSV rendering behaviour by adding your own preferred CSV options to the initializer, of course.

Hope this helps!

like image 141
florish Avatar answered Sep 23 '22 20:09

florish


This is an old question but here's an updated method for the custom Renderer for newer versions of Rails (currently using 3.2.11 and Ruby 1.9.3) taken from the ActionController::Renderers documentation: http://api.rubyonrails.org/classes/ActionController/Renderers.html#method-c-add

As florish said, create an initializer but add this code:

ActionController::Renderers.add :csv do |obj, options|
  filename = options[:filename] || 'data'
  str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
  send_data str, :type => Mime::CSV,
    :disposition => "attachment; filename=#{filename}.csv"
end

And use it as such:

def show
  @csvable = Csvable.find(params[:id])
  respond_to do |format|
    format.html
    format.csv { render :csv => @csvable, :filename => @csvable.name }
  end
end

I take no credit for the code above, it's straight from the documentation, but this worked for me in Rails 3.2.11 so pointing it out for people coming across this thread for the first time.

In my project I'm not using a to_csv method, I'm actually building the CSV manually first. So here's what mine looks like:

def show
  items = Item.where(something: true)
  csv_string = CSV.generate do |csv|
    # header row
    csv << %w(id name)
    # add a row for each item
    items.each do |item|
      csv << [item.id, item.name]
    end
  end
  respond_to do |format|
    format.csv { render :csv => csv_string, :filename => "myfile.csv" }
  end
end

You should obvious move the CSV creation code to some other class or model but putting it here inline just to illustrate.

like image 33
Jon Dean Avatar answered Sep 23 '22 20:09

Jon Dean