Raoot
Raoot

Reputation: 1771

ArgumentError on CSV output

i'm getting the following error when trying to generate a CSV:

ArgumentError in ProductsController#schedulecsv

wrong number of arguments (0 for 1)

My Products controller is set up as follows:

 def schedulecsv
 products = Product.find(:all)
 filename ="schedule_#{Date.today.strftime('%d%b%y')}"
  csv_data = CSV.generate do |csv|
    csv << Product.csv_header
      products.each do |p|
        csv << p.to_csv
      end
  end
  send_data csv_data,
  :type => 'text/csv; charset=iso-8859-1; header=present',
  :disposition => "attachment; filename=#{filename}.csv"
 end   

Does anyone have any pointers here? Driving me bonkers!

Thanks!

Upvotes: 0

Views: 355

Answers (1)

Naveed
Naveed

Reputation: 11167

From source of csv.rb place in /usr/lib/ruby/(version of your ruby gem)/csv.rb (on my machine)

Here is source code of CSV class's generate method

def CSV.generate(path, fs = nil, rs = nil, &block)
  open_writer(path, 'w', fs, rs, &block)
end

generate method require filename as parameter.it will make file with given name,but You are calling CSV.generate filename was missed

so you have to passed name of file in generate call!

filename ="schedule_#{Date.today.strftime('%d%b%y')}"
CSV.generate filename do |csv|
  csv << Product.csv_header
  products.each do |p|
    csv << p.to_csv
  end
end

Upvotes: 1

Related Questions