Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate CSV file from rails
    text
    copied!<p>I've been reading similar questions, but many of the answers are outdated or not clear enough for me. </p> <p>I'd like to be able to just do something like (in a controller action):</p> <pre><code>respond_to do |format| format.html format.csv end </code></pre> <p>I know I'd then need a view such as action.csv.erb</p> <hr> <p>So my questions are:</p> <p>1) What do I need to configure in rails to allow this to happen in general.</p> <p>2) How should I setup the CSV view to display some basic fields from a model?</p> <p><strong>UPDATE:</strong></p> <p>So I've tried to go the route of comma, I installed and vendored the gem. </p> <p>Then according to the read me, I threw this into my model (customized to my needs):</p> <pre><code>comma do user_id 'User' created_at 'Date' name 'Name' end </code></pre> <p>I then threw this in the control for the index action (according to the readme):</p> <pre><code> format.csv { render :csv =&gt; MyModel.limited(50) } </code></pre> <p>Then when accessing the index (not in CSV format) I receive the following ActionController Exception error:</p> <blockquote> <p>undefined method `comma' for</p> <h1></h1> </blockquote> <p>So then I googled that, and I read that I should put require 'comma' in my model.</p> <p>After doing that, I refreshed (my local index page), and the error changed to:</p> <blockquote> <p>no such file to load -- comma</p> </blockquote> <p>So at this point I decided it must not be finding the comma files obviously. So I copied the files from the vendored gem folder of comma, from comma's lib folder, to the rails lib folder. I then refreshed the page and landed on this error:</p> <blockquote> <p>uninitialized constant Error</p> </blockquote> <p>Then I pretty much gave up. </p> <blockquote> <p>The errors from the trace were: </p> <p>/Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:443:in <code>load_missing_constant' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in </code>const_missing' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:92:in `const_missing'</p> </blockquote> <p>Other notes, I have already installed FasterCSV</p> <p>Hope thats enough info :)</p>
 

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