9
12

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】画面上のボタンをクリックしてファイルをDLさせる際の注意点

Last updated at Posted at 2017-10-28

はじめに

Railsで画面上のボタンをクリックしてファイルをダウンロードさせる機能を実装していた際に気がついたことがあるので備忘録として記録しておきます。

問題点

send_file, send_dataを実行するアクションの呼び出しにlink_toを利用すると、ブラウザ上でリンクをクリックした際にリクエストが2重に送信される。

解決方法

アクションの呼び出しにlink_toではなくbutton_toを利用する。

詳細

ルーティング、コントローラ

http://xxx.xxx.xxx.xxx/download
にリクエストが来るとアプリ内で生成したファイルを渡す実装にしてました。

routes.rb
get  'download'  => 'get#download'
get_controller.rb
class GetController < ApplicationController

  def download
    send_data 'hogehoge',
              :filename => 'hoge.txt',
              :type => 'text/plain',
              :disposition => 'attachment'
  end

end

修正前

ビュー

downloadアクションの呼び出しにlink_toを利用して、CSS(bootstrap)で見た目をボタンにしてました。

xxx.html.erb
<%= link_to 'download', download_path, class: 'btn btn-info' %>
生成されたHTML
<a class="btn btn-info" href="/download">download</a>

button.png

ボタンクリック時の動作

ブラウザ上でボタンをクリックするとファイルがダウンロードされるため、正常に動作してるように見えますが、pumaのログを確認するとリクエストが2重に出力されてました。

Started GET "/download" for 127.0.0.1 at 2017-10-28 21:54:27 +0900
Processing by GetController#download as HTML
  Rendering text template
  Rendered text template (0.0ms)
Sent data hoge.txt (0.5ms)
Completed 200 OK in 1ms (Views: 0.3ms | ActiveRecord: 0.0ms)


Started GET "/download" for 127.0.0.1 at 2017-10-28 21:54:27 +0900
Processing by GetController#download as HTML
  Rendering text template
  Rendered text template (0.0ms)
Sent data hoge.txt (0.5ms)
Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)

修正後

ビュー

アクションの呼び出しにbutton_toを利用するように修正しました。

xxx.html.erb
<%= button_to 'download', download_path, method: :get, class: 'btn btn-info' %>
生成されたHTML
<form class="button_to" method="get" action="/download">
  <input class="btn btn-info" type="submit" value="download" />
</form>

button.png

ボタンクリック時の動作

pumaのログを確認するとリクエストが1回だけ出力されるようになりました。

Started GET "/download" for 127.0.0.1 at 2017-10-28 22:02:23 +0900
Processing by GetController#download as HTML
  Rendering text template
  Rendered text template (0.0ms)
Sent data hoge.txt (0.7ms)
Completed 200 OK in 2ms (Views: 0.5ms | ActiveRecord: 0.0ms)
9
12
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
9
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?