前提・実現したいこと
railsのディレクトリ内にある画像ファイルをダウンロードする際、指定するサブディレクトリがない場合にダウンロードを中止してalert等で「ダウンロード出来ない」表示を付けたい。
発生している問題
Dir.exist?でサブデレクトリがない場合、リンク先のviewのhtmlソースがchromeHTMLdocumentでダウンロードされてしまう。
該当のソースコード
controller
1directory_to_zip = "#{Rails.root}/public/downloads/name_no#{@index}" 2 output_file = "#{Rails.root}/public/zips/name_no#{@index}.zip" 3 zip_file_generator = ZipFileGenerator.new(directory_to_zip, output_file) 4 zip_file_generator.write 5 if Dir.exist?(directory_to_zip) 6 send_file(output_file, filename: "name_no#{@index}.zip", disposition: 'attachment', stream: true) 7 else 8 return
view
1<td><%= link_to 'download', download_index_path(index.id), download:"", class:"btn btn-default" %></td>
画面下部の表示(画像)
試したこと
例外処理のbigin/rescueを付け足そうとしましたが、理解不足で出来ませんでした。
return
ではなくexit!
としてもdownload自体は実行され以下の表示になります。
これでも良いのかもしれませんが出来るならばdownload停止とともに「ディレクトリ(画像)がないのでdownloadが出来ません」等の表示が出るようにしたい。
error
1download 失敗 - サーバーに問題が発生しました
よろしくおねがいします。
あなたの回答
tips
プレビュー