Topic: Generating a blank CSV

I'm writing an application that will collect data from conference delegates at a public laptop.  I have all the details stored in a mysql database and at the end of each day I want to be able to export the data to a CSV file.  Here is the export function I have written in the applications only controller (PartnersController):

def export
    @partner = Partner.all
    csv_string = CSV.generate do |csv|
      columns = ["title", "first_name", "laste_name"]

      csv << columns

      @partner.each do |partner|                
        csv << [partner.title, partner.first_name, partner.last_name ]
      end

      filename = "ipartner-#{Time.now.to_date.to_s}.csv"    
      send_data(csv_string, :type => 'text/csv; charset=utf-8; header=present', :filename => filename)  
    end
  end

The above just generates a blank CSV file.  Can anyone see what I'm doing wrong?

Re: Generating a blank CSV

Ok, I solved it.  I was getting mixed up with where to put end statements and which variable names should be plural.  Here is the code that works:

def export
    @partners = Partner.find(:all)

    csv_string = CSV.generate do |csv|
      csv << ["title", "first_name", "last_name"]

      @partners.each do |partner|
        csv << [partner.title, partner.first_name, partner.last_name]
      end
    end

    filename = "ipartner-#{Time.now.to_date.to_s}.csv"    
    send_data(csv_string, :type => 'text/csv; charset=utf-8; header=present', :filename => filename)
  end

Re: Generating a blank CSV

Hi, I am trying to export a CSV using a code more or less same as abobe. The only difference is I am using AJAX post to send Ids from the display(all checked rows). With those ids i am retrieving results. So far so good. Now the only issue is I am not able to download the data as a CSV file. Instead, its showing me all the retrieved data as an alert message!!!

Any ideas why this is happening?? or where i am going wrong???