Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another way to do this without using FasterCSV:</p> <p>Require ruby's csv library in an initializer file like config/initializers/dependencies.rb</p> <pre><code>require "csv" </code></pre> <p>As some background the following code is based off of <a href="http://railscasts.com/episodes/111" rel="noreferrer">Ryan Bate's Advanced Search Form</a> that creates a search resource. In my case the show method of the search resource will return the results of a previously saved search. It also responds to csv, and uses a view template to format the desired output.</p> <pre><code> def show @advertiser_search = AdvertiserSearch.find(params[:id]) @advertisers = @advertiser_search.search(params[:page]) respond_to do |format| format.html # show.html.erb format.csv # show.csv.erb end end </code></pre> <p>The show.csv.erb file looks like the following:</p> <pre><code>&lt;%- headers = ["Id", "Name", "Account Number", "Publisher", "Product Name", "Status"] -%&gt; &lt;%= CSV.generate_line headers %&gt; &lt;%- @advertiser_search.advertisers.each do |advertiser| -%&gt; &lt;%- advertiser.subscriptions.each do |subscription| -%&gt; &lt;%- row = [ advertiser.id, advertiser.name, advertiser.external_id, advertiser.publisher.name, publisher_product_name(subscription), subscription.state ] -%&gt; &lt;%= CSV.generate_line row %&gt; &lt;%- end -%&gt; &lt;%- end -%&gt; </code></pre> <p>On the html version of the report page I have a link to export the report that the user is viewing. The following is the link_to that returns the csv version of the report:</p> <pre><code>&lt;%= link_to "Export Report", formatted_advertiser_search_path(@advertiser_search, :csv) %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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