Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been struggling with the exact same problem. I might have found a solution.</p> <p>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): <a href="https://github.com/rails/rails/blob/v3.0.10/actionpack/lib/action_controller/metal/renderers.rb">https://github.com/rails/rails/blob/v3.0.10/actionpack/lib/action_controller/metal/renderers.rb</a></p> <p>First, add a simple <code>as_csv</code> method to your model definition:</p> <pre><code>class Modelname &lt; ActiveRecord::Base # ... def as_csv attributes end end </code></pre> <p>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 <code>as_csv</code> comes from Rails' <code>as_json</code> method, which return a Ruby object that is used by <code>to_json</code> to generate the actual JSON (text) output.</p> <p>With the <code>as_csv</code> method in place, put the following code in a file in <code>config/initializers</code> inside your app (name it <code>csv_renderer.rb</code>, for example):</p> <pre><code>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 &lt;&lt; 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 </code></pre> <p>And finally, add CSV support to your controller code:</p> <pre><code>class ModelnamesController &lt; ApplicationController respond_to :html, :json, :csv def index @modelnames = Modelname.all respond_with(@modelnames) end # ... end </code></pre> <p>The initializer code is largely based on the :json and :xml behaviour from the Rails source code (see link above).</p> <p>Currently, the <code>options</code> hash passed to the block doesn't get passed to the <code>to_csv</code> 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 <code>to_csv</code>. You can change the default CSV rendering behaviour by adding your own preferred CSV options to the initializer, of course.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload