如何使用send_file下载文件?

use*_*562 3 ruby-on-rails sendfile ruby-on-rails-4

有人可以告诉我如何下载文件send_file

image.jpg里面有一个文件app/assets/images.我在我的控制器中试过这个:

def download
    send_file ("#{Rails.root}/public/images/image.jpg")
end

def download
    send_file ("#{Rails.root}/assets/images/image.jpg")
end

def download
    send_file ("#{Rails.root}/images/image.jpg")
end

def download
    send_file ("/public/images/image.jpg")
end

def download
    send_file ("/assets/public/images/image.jpg")
end

def download
    send_file ("/assets/images/image.jpg")
end
Run Code Online (Sandbox Code Playgroud)

对于每条路径,它说:

ActionController::MissingFile in HomeController#download
Cannot read file 'some_path'
Run Code Online (Sandbox Code Playgroud)

这可能是个问题?谢谢!

Coe*_*ulf 9

尝试:

IMAGES_PATH = File.join(Rails.root, "public", "images")

def download
  send_file(File.join(IMAGES_PATH, "image.jpg"))
end
Run Code Online (Sandbox Code Playgroud)