2011-07-05 7 views
5

ファイルダウンロードしたい:FirefoxとChromeを使ってCSVは、Safariのビューでレンダリングするが、私はそれが私がカスタムMIMEタイプ設定している

respond_to do |format| 
    format.html 
    format.csv { render :csv => csv_code} 
    end 

:私のコントローラで

ActionController::Renderers.add :csv do |csv, options| 
    self.content_type ||= Mime::CSV 
    self.response_body = csv.respond_to?(:to_csv) ? csv.to_csv : csv 
end 

とrespond_toブロックを、.CSVがダウンロードされたファイルをレンダリングします。 Safariを使用して.CSVは、ビューとしてレンダリングされます。どのように私はこれを変更し、ファイルとしてダウンロードするには、それを強制することができますか?

enter image description here

答えて

9

が、これは動作しない場合は、

send_file "path/to/file.csv", :disposition => "attachment" 
+0

感謝。 response.headers方法は素晴らしい仕事します。 –

0

私はこれを持っている方法で使用してみてください

respond_to do |format| 
    format.html 
    format.csv do 
     response.headers['Content-Type'] = 'text/csv' 
     response.headers['Content-Disposition'] = 'attachment; filename=thefile.csv'  
     render :csv => csv_code 
    end 
end 

をお試しください:

は、問題のスクリーンショットを参照してください。古いレール2のアプリで作業することは send_dataの代わりを使用していますコントローラで。例えば:

def csv 
    ... # build data 
    send_data csv_data_as_string, :filename => "#{filename}.csv", :type => 'text/csv' 
end 
関連する問題