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?
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: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 byto_json
to generate the actual JSON (text) output.With the
as_csv
method in place, put the following code in a file inconfig/initializers
inside your app (name itcsv_renderer.rb
, for example):And finally, add CSV support to your controller code:
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 theto_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 toto_csv
. You can change the default CSV rendering behaviour by adding your own preferred CSV options to the initializer, of course.Hope this helps!
First create a renderer for the CSV mime type in
config/initializers/csv_renderer.rb
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:You can then pass an ActiveRecord relation to respond_with:
One of the possible solutions is to implement another view with all data you want.
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:
And use it as such:
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:
You should obvious move the CSV creation code to some other class or model but putting it here inline just to illustrate.
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 tryI 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 ofresources
. 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.If that does not apply, run
rake routes
to see if your routes are configured correctly.