我在控制器中有一个方法export_csv。
def export_csv
if params[:from_date].present? && params[:to_date].present?
@users = User.where("created_at between ? and ?", params[:from_date], params[:to_date])
if !@users.blank?
users_csv = User.to_excel(@users)
send_data(users_csv, :type => 'text/csv', :filename => 'users.csv')
flash.now[:success] = "Successfully downloaded the report!"
else
flash.now[:notice] = "No records over selected duration!"
end
else
flash.now[:notice] = "Select from and to date.."
end
end
文件已下载,但页面未刷新或重新加载。因此,即使在下载文件后,flash消息仍保持在页面上。
我浏览过几个网站,发现send_data会自动渲染视图,因此无法使用其他重定向或渲染。
有没有办法在send_data之后重新加载页面。?
答案 0 :(得分:1)
send_data
设置整个服务器响应,因此浏览器只接收CSV文件,而不是网页。这就是您的Flash消息未显示的原因。另一种方法是生成一个临时CSV文件(带有随机名称)并提供一个链接:
def export_csv
if params[:from_date].present? && params[:to_date].present?
@users = User.where("created_at between ? and ?", params[:from_date], params[:to_date])
if !@users.blank?
#Create temporary CSV report file and get the path to it.
csv_file_path = create_csv_file(User.to_excel(@users))
#Change the flash message a bit for requesting the user
#to click on a link to download the file.
flash.now[:success] = "Your report has been successfully generated! Click <a href='#{csv_file_path}'>here</a> to download".html_safe
else
flash.now[:notice] = "No records over selected duration!"
end
else
flash.now[:notice] = "Select from and to date.."
end
end
当然你应该实现函数create_csv_file
。为了避免在服务器中保留旧文件,您可以实现一种新方法,例如download_report
将读取文件,使用send_data
发送回客户端并最终将其删除。
修改强>
上述功能的伪代码:
require 'tempfile'
def create_csv_file(data)
#Create a temporary file. If you omit the second argument of Tempfile.new
#then the OS's temp directory will be used.
tmp = Tempfile.new('report', 'my/temp/dir')
tmp.write(data)
tmp.close
return tmp.path
end
#Method in controller for downloading the file. I omit checks and/or security issues.
def download_report
#Warning: a mechanism should be implemented to prevent the remote
#client from faking the path and download other files.
#A possible solution would be providing not only the file path but also a
#hash with a secret key to validate the path. Function create_csv_file()
#should, then, return a hash in addition of a path.
path = params[:report]
file = File.open(path, "rb")
contents = file.read
file.close
send_data(contents , :type => 'text/csv', :filename => 'users.csv')
file.unlink #Delete file
end