# desc "Explaining what the task does" # task :universal_table do # # Task goes here # end namespace :universal_table_tasks do task :prepare_download,[:utable_id, :url] => :environment do |task,args| id = args.utable_id I18n.locale = :zh_tw table = UTable.find(id) ac = ActionController::Base.new() url = "http://#{args.url}" xlsx = ac.render_to_string handlers: [:axlsx], formats: [:xlsx], template: "utable_export/export", locals: {table: table, site_in_use_locales: Site.first.in_use_locales, url: url} dirname = "public/uploads/utable_export/#{id}" FileUtils.mkdir_p(dirname) unless File.exists?(dirname) f = "#{dirname}/#{table.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')}.xlsx" if File.exists?(f) File.delete(f) end file = File.open(f, "w") xlsx.force_encoding("utf-8") file.write(xlsx) end end