éçºç°å¢ã«ä½¿ã£ã¦ããThin web serverã«ããã¦send_fileã®å¼ã³åºãç´å¾ã«ãsend_fileã§éã£ããã¡ã¤ã«ãåé¤ããã¨Internal serverã¨ã©ã¼ã§æ¢ã¾ãã
追è¨ï¼æ¬çªç°å¢ï¼Apache + Passengerï¼ã®å ´åãInternal Serverã¨ã©ã¼ã«ã¯ãªããªããããã¦ã³ãã¼ããããã¡ã¤ã«ãµã¤ãºã0 byteã«ãªãã
æ¬æ
ãããã¼ã¸ã§ããã¿ã³ãã¯ãªãã¯ããã¨ãã¡ã¤ã«ã®ãã¦ã³ãã¼ããå§ã¾ããã¼ã¸ãä½ãã¨ãã以ä¸ã®ããã«ããã¨WebRickã§ã¯ã¨ã©ã¼ã¨ãªããApache+passengerã ã¨ã¡ããã¨åããdownloadsã³ã³ããã¼ã©ã¼ã®showã§ãã¿ã³ãã¯ãªãã¯ãããã¨ãdownloads#csvfileãå¼ã³åºããã¦csvãã¡ã¤ã«ãä¸åº¦ä½æãããããsend_fileã§éãã¨ããã¹ã¯ãªããã
app/views/downloads/show
<%= form_tag({:action => "csvfile"}, {:method => :get}) do -%> <%= submit_tag(_("ãã¦ã³ãã¼ãéå§")) -%> <% end -%>
app/controllers/downloads_controller.rb
def csfvile sourceAry = Array.new sourceAry.push(["hello", "world"]) sourceAry.push(["hello", "world, world!"]) filepath = "tmp.csv" CSV.open(filepath, "wb") do |writer| sourceAry.each do |line| writer << line end end if File.exist?(filepath) send_file(filepath, {:type => 'text/csv', :filename => "examinees_#{params[:id]}.csv"}) File.delete(filepath) # ãããã¨ã©ã¼ãå¼ãèµ·ãã end end
downloads#csvfile ã«ããã¦ãsend_fileã®çµäºå¾ã«ä½æããcsvãã¡ã¤ã«ãåé¤ãã¦ããã®ã ãã©ãWebRickã®å ´åã¨ã©ã¼ã«ãªããWebãã©ã¦ã¶ä¸ã®è¡¨ç¤ºã¯ãInternal server errorãããã°ã®ã¡ãã»ã¼ã¸ã¯ä»¥ä¸ã®ã¨ããã
!! Unexpected error while processing request: No such file or directory - /home/hogehoge/railsroot/tmp.csv
Happy Elements Labs:Rubyist必携 pry-railsを使いこなせば幸せになれるãåèã«ãpry-debuggerã§é次å®è¡ãã¦ã¿ãã¨ãããdef csvfile ã®çµäºå¾ã«ã¨ã©ã¼ãçºçãã¦ãããã¨ãããã£ããã¨ã©ã¼ã®çç±ã¯å®éã«ãã¡ã¤ã«ãéããã¨ããã¨ãã«ããã®ãã¡ã¤ã«ãåå¨ãã¦ããªããããã¤ã¾ããsend_fileãå¼ã³åºããã¨ãã«ãã¡ã¤ã«ã®éä¿¡ãè¡ããã¦ããã®ã§ã¯ãªãã¨ãããã¨ã
æ¬çªç°å¢ã¨ãã¦ä½¿ã£ã¦ããApache + passenger ã§ã¯ãã®ã¨ã©ã¼ã¯èµ·ãããªãã
解決æ³ãããããªããã©ã¨ããããã¡ã¢ã