tags:

views:

964

answers:

1

What am I trying to do: export data to csv.

I have a form which allows user to select the format (from a drop down menu). So based on the selection of the format the ouput is displayed using a ajax call. Works fine for html but when I select the format as csv I don't see any pop up on the screen (asking to save or open the file) and neither any file gets downloaded directly.

I tried using Fastercsv (but the problem is that I don't see any pop up asking me whether I want to save or open the file) and CSV::Writer where I get this error message on the console.

NoMethodError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.bytesize):
  actionpack (2.3.4) lib/action_controller/streaming.rb:142:in `send_data'

Code using Fastercsv:

   def export_to_csv
    csv_string = FasterCSV.generate(:col_sep => ",") do |csv|
      members = ["Versions / Project Members"]
      members_selected.each {|member| members << Stat.member_name(member)}
      Stat.project_members(project).each {|user| members << user.name}
      csv << ["some text", "text 2", "text 3"]
    end
    return csv_string
   end

and this is how I am sending the data:

  send_data(export_to_csv,:type => 'text/csv; charset=iso-8859-1; header=present',
                     :disposition => "attachment", :filename => "filename.csv")

I see the response as "some text, text 2, text 3" in the firebug console but no pop up asking whether I want to save or open the file.

This is what I am doing using CSV::Writer:

   def export_to_csv
     report = StringIO.new
      CSV::Writer.generate(report, ',') do |csv|
        csv << ['c1', 'c2']
      end
  end

and call it as:

 send_data(export_to_csv,:type => 'text/csv; charset=iso-8859-1; header=present',
                     :disposition => "attachment", :filename => "filename.csv")

This is the error which is thrown on the console:

NoMethodError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.bytesize):
  actionpack (2.3.4) lib/action_controller/streaming.rb:142:in `send_data'
A: 

send_data is trying to reference an object that is out of scope. Check your closing 'end' statement

Ray