Rails 5.1
在app / controllers / fw_exports_controller.rb中,我有:
def download_csv
followed = Followed.find(params[:followed_id])
file_path = "#{Rails.root.to_s}/csv_container/#{followed.screen_name}_locations.csv"
send_file (file_path), :type => "text/csv", :disposition => 'inline'
redirect_to root_path, notice: t('fw_exports.spreadsheet_export.download_ok')
end
执行此操作时,我在日志文件中看到以下内容:
Started POST "/download_csv" for 75.151.110.193 at 2017-12-12 22:31:56 +0000
Processing by FwExportsController#download_csv as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "followed_id"=>"followed.1510895564.0013862", "button"=>""}
Followed Load SELECT `followeds`.* FROM `followeds` WHERE `followeds`.`id` = 'followed.1510895564.0013862' LIMIT 1
Sent file /home/fwtest/rails/csv_container/wm_locations.csv (0.2ms)
Redirected to http://fwtest.myapp.com/
我确认/home/fwtest/rails/csv_container/wm_locations.csv在那里并且它是CSV,但没有任何内容下载到我的本地PC。
任何想法我做错了什么?
答案 0 :(得分:1)
删除redirect_to
。如果您同时拥有send_file
和redirect_to
,则会发送两个回复 - 文件和重定向。您正在重定向到root_path
,并且无法开始下载。