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

后端 未结 6 2310
时光取名叫无心
时光取名叫无心 2020-12-28 17:29

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:

相关标签:
6条回答
  • 2020-12-28 17:36

    First create a renderer for the CSV mime type in config/initializers/csv_renderer.rb

    ActionController::Renderers.add :csv do |collection, options|
      self.content_type ||= Mime::CSV
      self.headers['Content-Disposition'] = "attachment; filename=#{options[:filename]}.csv" if options[:filename]
      self.response_body = collection.to_csv
    end
    

    Then add a to_csv method to your model. If your data is an array or hash you might consider creating a new class for that collection with its own to_csv and to_json methods, instead of having everything in the controller. If its an ActiveRecord model you can use the following in an initializer:

    require 'csv'
    
    module CsvRenderer
      def to_csv(options={})
        columns = column_names
        columns += options[:include] if options[:include]
        CSV.generate do |csv|
          csv << columns
          all.pluck(*columns).each do |row|
            csv << row
          end
        end
      end
    end
    
    ActiveRecord::Base.extend CsvRenderer
    

    You can then pass an ActiveRecord relation to respond_with:

    def index
      respond_with(Item.all, filename: 'items')
    end
    
    0 讨论(0)
  • 2020-12-28 17:40

    I think your model would have to have a to_csv method that returns the attributes as csv.

    After that, if Rails doesn't call the to_csv method implicitly, I would try

    respond_with TripReport.new.to_csv
    
    0 讨论(0)
  • 2020-12-28 17:44

    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!

    0 讨论(0)
  • 2020-12-28 17:48

    One of the possible solutions is to implement another view with all data you want.

    # controller code
    respond_to :html, :csv
    
    def index
      respond_with Person.all
    end
    
    # view
    views/persons/index.csv.erb
    
    0 讨论(0)
  • 2020-12-28 17:51

    I ran into what I would guess is a similar problem to what you were experiencing, Oliver. I realized that in my routes file I was using resource instead of resources. I do not know if you have other actions in your Admin::ReportsController class or what your routes file looks like, but this is how I would tackle the problem if Reports has the standard REST actions.

    scope :module => 'Admin' do
      resources :reports do
        get :trips, :on => :collection
      end
    end
    

    If that does not apply, run rake routes to see if your routes are configured correctly.

    0 讨论(0)
  • 2020-12-28 18:03

    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.

    0 讨论(0)
提交回复
热议问题