2014-05-08 48 views
0

控制器如何傳遞額外的參數to_csv方法

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: 「\ t」 的,:START_DATE => 「日期」),文件名:「顧客。 XLS'}

然後得到錯誤,如:未知的選項起始日期

+0

那麼問題是什麼?將這些參數放入第5行的數組中。 –

+0

給我例子,self.to_csv(options = {}),在這裏我想從控制器獲取值 –

回答

2

試試這個:

format.csv { send_data @find_customer.to_csv({}, '2014-05-08', '2014-05-10'), :filename => "customer" + ".csv" } 

model

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