在Rails应用程序代码库中创建了本地文件副本

时间:2016-10-31 02:55:16

标签: ruby ruby-on-rails-3 ruby-on-rails-3.2

我正在使用rails 3.2.22和ruby 2.2。

我正在创建一个文本文件,我想使用send_to发送下载文件。我的代码按预期工作,但每次我点击该操作时,都会在我的应用程序根文件夹中创建该文件的本地副本。我不想要这个,文件应该直接去下载文件夹。我做错了吗?

def save_trunk_logs
    data = ""
    file = "test.txt"
    trunk_logs = some data
    File.open(file, "w") do |aFile|
      aFile.write("Trunk Name : #{trunk_name}\n")
      aFile.write("*"*100)
      aFile.write("\n")
      aFile.write("Time Stamp"+"\t"+"Log Message\n")
      trunk_logs.each do |msg|
      text =format_log_messages msg
        data << "#{data}\n"
     end
  end
  send_file file, :type => 'application/text; charset=UTF-8', :disposition => 'attachment'
end

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

试试这个。

  def save_trunk_logs
    data = ""
    io = StringIO.new
    trunk_logs = some data
    io.write("Trunk Name : #{trunk_name}\n")
    io.write("*"*100)
    io.write("\n")
    io.write("Time Stamp"+"\t"+"Log Message\n")
    trunk_logs.each do |msg|
      text =format_log_messages msg
      data << "#{data}\n"
    end
    send_data io.string, :type => 'application/text; charset=UTF-8', :disposition => 'attachment'
  end