控制器
format.csv { send_data @find_customer.to_csv, :filename => "customer" + ".csv" }
format.xls { send_data @find_customer.to_csv(col_sep: "\t"), filename: 'customer.xls'}
模型
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << ["SHW#", "LeadId", "Fullname", "Address", "City", "State", "Zip", "Phone", "Email", "Created-At"]
all.each do |customer|
csv << [customer.id, customer.leadid, "#{customer.first_name} #{customer.last_name}", customer.customer_address, customer.customer_city, customer.customer_state, customer.customer_zip, customer.customer_phone, customer.email, customer.created_at.strftime("%m/%d/%Y")]
end
end
end
如何在to_csv方法中传递额外参数。我想在to_csv方法中传递start_date和end_date所以我该怎么办?
更新1
如果我通过
format.xls {send_data @ find_customer.to_csv(col_sep:&#34; \ t&#34;,:start_date =&gt;&#34; date&#34;),filename:&#39; customer.xls&# 39;}
然后得到如下错误:未知选项start_date
答案 0 :(得分:2)
试试这个:
format.csv { send_data @find_customer.to_csv({}, '2014-05-08', '2014-05-10'), :filename => "customer" + ".csv" }
模型
def self.to_csv(options = {}, start_date = '', end_date = '')
CSV.generate(options) do |csv|
csv << ["SHW#", "LeadId", "Fullname", "Address", "City", "State", "Zip", "Phone", "Email", "Created-At", "Start", "End"]
all.each do |customer|
csv << [customer.id, customer.leadid, "#{customer.first_name} #{customer.last_name}", customer.customer_address, customer.customer_city, customer.customer_state, customer.customer_zip, customer.customer_phone, customer.email, customer.created_at.strftime("%m/%d/%Y"), start_date, end_date]
end
end
end