我正在使用的代码:
在视图文件中:
<%= link_to "Download",download_image_path(image) %>
在控制器中:
def download @image = Image.find(params[:id]) send_file "#{RAILS_ROOT}/public" + @image.attachment.url end
我收到一个错误:
Cannot read file /Users/mohit/projects/my_app/public/system/attachments/4/original/Screen Shot 2011-11-04 at 3.14.03 PM.png?1320582022
解决方法
问题是:
我在用
@image = Image.find(params[:id]) send_file "#{RAILS_ROOT}/public" + @image.attachment.url
它应该是
@image = Image.find(params[:id]) send_file @image.attachment.path
PS:确保验证图像/记录是否存在.