mercredi 24 février 2016

Export csv in activeadmin new page(no model)

I have created new page in active admin as details in my new page I want to display the table which is having data from another model(miles), Problem is I want to export all data to csv form? Check the following code I'm facing problem with exporting..

 ActiveAdmin.register_page "Details" do
menu false
  page_action :csv, method: :get do
     # transactions = Transaction.all.page(params[:page]).per(Transaction::ADMIN_PAGE_LIMIT) 
               @milestones =  Milestone.all
      @collection =  @milestones.select{|a| a.milestone_status == false}
      @month_collection = @collection.select{ |a| a.delivery_date.to_date.strftime('%m%Y') == Date.today.strftime('%m%Y') }
     transactions = @month_collection
     csv = CSV.generate( encoding: 'Windows-1251' ) do |csv| 
     # add headers 
     csv << ["admin_user", "project", "client", "milestone_name", "delivery_date", "milestone_payment", "received_amount"] 
     transactions.each do |transaction|
      csv << [ transaction[:admin_user], transaction[:project], transaction[:milestone_name], transaction[:delivery_date], transaction[:milestone_payment], transaction[:received_amount]] 
         end 
     end
      # send file to user 
      send_data csv.encode('Windows-1251'), type: 'text/csv; charset=windows-1251; header=present', disposition: "attachment; filename=transactions_#{DateTime.now.to_s}.csv" end
       action_item do
        link_to "Export to CSV", method: :get 
    end 
end

Aucun commentaire:

Enregistrer un commentaire