Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6626581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:54:33+00:00 2026-05-25T21:54:33+00:00

I have an object that has a to_csv method and I want to pass

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:54:33+00:00Added an answer on May 25, 2026 at 9:54 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use/reuse C++ object with Objective-C. I have a hello.h that has
In C#, is it possible to have an object that has multiple method signatures
I have an object that has a multi-part key and I am struggling to
Lets say I have an object that has stringProp1, stringProp2. I wish to store
I have an object in SQL (A) that has a many to many relationships
I have a string I need to feed to a com object that has
Suppose I have a UserControl whose DataContext is set to an object that has
I have two tables: object that has object_id column and avalues that have object_id
I have an object that has a list of abstract 'aninamls'. i.e. var animals
I am using an asp:Calander and I have an object that has a beginning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.