Ruby on rails 升级到Rails 3.2.11后,Rails应用程序无法再下载文本文件-找不到文件

Ruby on rails 升级到Rails 3.2.11后,Rails应用程序无法再下载文本文件-找不到文件,ruby-on-rails,Ruby On Rails,短版本(TLDR): 下面的下载文件脚本在Rails 3.2.6中运行良好,但在Rails 3.2.11中不起作用。 长版本: 我以前有一个很好的下载文本文件功能,但现在当我尝试该操作时,我得到了以下信息: This webpage is not found No webpage was found for the web address: http://localhost:3000/file_download Error 6 (net::ERR_FILE_NOT_FOUND): The fi

短版本(TLDR): 下面的下载文件脚本在Rails 3.2.6中运行良好,但在Rails 3.2.11中不起作用。

长版本:

我以前有一个很好的下载文本文件功能,但现在当我尝试该操作时,我得到了以下信息:

This webpage is not found 
No webpage was found for the web address: http://localhost:3000/file_download
Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.
My routes.rb指定了正确的/file\u下载路径。除了将Rails版本升级到3.2.11之外,我没有做任何更改。我的下载脚本也没有改变;还是这个,

def download_file
    filename = 'my_memories.txt'
    file = File.open(filename, 'w') # creates a new file and writes to it

    current_user.memories.order('date DESC').each do |m|
      # Write the date, like 1/21/2012 or 1/21/2012~
      file.write m.date.strftime("%m/%d/%Y")
      file.write '~' unless m.exact_date
      file.write " #{m.note}" if m.note
      file.puts

      # Write the content
      file.puts m.content
      file.puts # extra enter
    end

    send_file file
    file.close # important, or no writing will happen
    File.delete(filename)
  end
我在控制台中看到的错误如下:

ERROR Errno::ENOENT: No such file or directory - my_memories.txt
我已经研究并尝试了这个解决方案,但没有成功:我会得到一个不同的错误

private method `puts' called for #<String:0x00000104551838>
在下载文件方法的开头

出了什么问题?在Rails版本更改之前,一切正常


提前感谢。

我找到了问题的答案,部分原因是这里的人:

基本上,send_文件以前被滥用过(我在网上的某个地方看到了旧方法)。应该传递一个文件路径

工作代码:

  def download_file
    filepath = "#{Rails.root}/public/downloaded_memories/my_memories.txt"

    file = File.open(filepath, 'w') # creates a new file and writes to it

    current_user.memories.order('date DESC').each do |m|
      # Write the date, like 1/21/2012 or 1/21/2012~
      file.write m.date.strftime("%m/%d/%Y")
      file.write '~' unless m.exact_date
      file.write " #{m.note}" if m.note
      file.puts

      # Write the content
      file.puts m.content
      file.puts # extra enter
    end

    send_file filepath # make sure to send the file path, not the file name
    file.close # important, or no writing will happen
    # File.delete(filepath) # cannot delete this since the request would not then try to send a file path that doesn't exist
  end

一个可能更好的方法是使用send_数据,而不是完全使用send_文件。我自己也在学习如何使用send_文件。你把这个方法放在哪里?我有一个名为Resource.rb的模型,其中有一列名为“document”。我想导航到资源并下载相关文档。我在ResourcesController中放置了一个类似的下载文件方法,但是当我单击链接时出现了一个错误:没有ID无法找到资源。感谢您的帮助。
  def download_file
    filepath = "#{Rails.root}/public/downloaded_memories/my_memories.txt"

    file = File.open(filepath, 'w') # creates a new file and writes to it

    current_user.memories.order('date DESC').each do |m|
      # Write the date, like 1/21/2012 or 1/21/2012~
      file.write m.date.strftime("%m/%d/%Y")
      file.write '~' unless m.exact_date
      file.write " #{m.note}" if m.note
      file.puts

      # Write the content
      file.puts m.content
      file.puts # extra enter
    end

    send_file filepath # make sure to send the file path, not the file name
    file.close # important, or no writing will happen
    # File.delete(filepath) # cannot delete this since the request would not then try to send a file path that doesn't exist
  end