6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Rails】CSVダウンロード後の画面のリロード

Last updated at Posted at 2019-07-27

あるデータの一覧のCSVダウンロードした後に画面の情報が変わっていることを確認させる必要があったときの開発メモ。

controllerからだとリダイレクトは出来ない。

自分の考えとしてはcontroller内のアクションにダウンロードした後にリダイレクトをさせれば良いと考え以下のように記載。

def download
    respond_to do |format|
      format.csv do
        send_data render_to_string, filename: "#{__method__}_#{Time.zone.now.strftime('%Y%m%d')}.csv", type: :csv
      end
    end
    redirect_to hoge_url
end

ただこれだとDouble renderとなり起こられてしまう。

解決 jsでリダイレクトさせる

slim

.text-right
  = link_to 'CSV', download_path(format: :csv),
    class: 'btn btn-link', id:'download-csv', target: '_blank'

js

  $('#download-csv').click -> location.reload()

参考: http://tshidax.hatenablog.com/entry/2017/09/11/120000

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?