title: ERROR IOError: closed stream in Sinatra Ruby app when writing to CSV file on AJAX post
body: <p>Here's my /mail_to route:</p>
  
  <pre><code>post '/mail_to' do
      Pony.mail :subject =&gt; " New Inquiry Received",
                :body    =&gt; erb(:email),
                :to =&gt; params["Email Address"],'
  
      CSV.open('registrations.csv', 'a+') do |csv|
        csv &lt;&lt; ['FIRST NAME', 'LAST NAME', 'EMAIL', 'PHONE', 'ADDRESS', 'CITY', 'STATE', 'COUNTRY', 'RESIDENCE', 'PRICE RANGE', 'HEARD THROUGH', 'IMPT. FACTOR', 'COMMENTS'] if csv.read.size == 0
        csv &lt;&lt; [
                 params["First Name"],
                 params["Last Name"],
                 params["Email Address"],
                 params["Phone Number"], 
                 params["Address"],
                 params["City"],   
                 params["State"],   
                 params["Country"],  
                 params["Residence"],
                 params["Price Range"], 
                 params["Heard About Us Through"],  
                 params["Most Important Factor"], 
                 params["Comments"]  
               ]
      end
  end
  </code></pre>
  
  <p>And this is where /mail_to is called in my JS: </p>
  
  <pre><code>            // register form handler
              $( "#reg_form" ).submit(function(event) {
                    // Stop form from submitting normally
                     event.preventDefault();
                     //ajax request, show modal if form is posted successfully
                     $.post( "/mail_to", $("#reg_form").serialize(), function(data) {
                          $('#reg_modal').foundation('reveal', 'open');
                     }).fail(function() {
                          $('#reg_error_modal').foundation('reveal', 'open');
                     });       
              });
  </code></pre>
  
  <p>I'm seeing this error in my Sinatra app when submitting the form (this wasn't happening before adding the csv writing part):</p>
  
  <pre><code>127.0.0.1 - - [06/Jan/2014 17:26:09] "POST /mail_to HTTP/1.1" 200 - 1.1071
  [2014-01-06 17:26:09] ERROR IOError: closed stream
  /Users/sikandarshukla/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.5.2/lib/rack/lint.rb:687:in `close'
  /Users/sikandarshukla/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.5.2/lib/rack/body_proxy.rb:16:in `close'
  /Users/sikandarshukla/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.5.2/lib/rack/chunked.rb:34:in `close'
  /Users/sikandarshukla/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.5.2/lib/rack/handler/webrick.rb:76:in `ensure in service'
  /Users/sikandarshukla/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.5.2/lib/rack/handler/webrick.rb:76:in `service'
  /Users/sikandarshukla/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/sikandarshukla/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/sikandarshukla/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
  </code></pre>
  
  <p>I'm always seeing the "$('#reg_error_modal')", yet the email is going through and the CSV is being written to. Any ideas why the AJAX request is 'failing'? What am I doing wrong when writing to CSV? I've tried using 'a', 'wb' and 'a+' with CSV.open..</p>