0

ショートバージョン(TLDR): 以下のdownload_fileスクリプトは、Rails 3.2.6では正常に機能しますが、Rails3.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.

私のroutes.rbには正しい/file_downloadルートが指定されています。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

そして、RubyのFile.openで「そのようなファイルやディレクトリはありません-text.txt(Errno :: ENOENT)」というエラーが表示され、その解決策を試しましたが、うまくいきませんでした。別のエラーが発生します。

private method `puts' called for #<String:0x00000104551838>

私がやったら

puts Dir.pwd.

ファイルのダウンロードメソッドの開始時。

何がうまくいかない可能性がありますか?Railsのバージョンが変更されるまでは問題なく動作しました。

前もって感謝します。

4

1 に答える 1

0

https://github.com/rails/rails/issues/9086:https://github.com/rails/rails/issues/9086の人のおかげで、自分の問題に対する答えを見つけました。

基本的に、send_fileは以前に悪用されていました(古いメソッドをオンラインのどこかで見ました)。ファイルパスを渡す必要があります。

作業コード:

  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
于 2013-01-27T03:55:49.987 に答える