Question

Here's my /mail_to route:

post '/mail_to' do
    Pony.mail :subject => " New Inquiry Received",
              :body    => erb(:email),
              :to => params["Email Address"],'

    CSV.open('registrations.csv', 'a+') do |csv|
      csv << ['FIRST NAME', 'LAST NAME', 'EMAIL', 'PHONE', 'ADDRESS', 'CITY', 'STATE', 'COUNTRY', 'RESIDENCE', 'PRICE RANGE', 'HEARD THROUGH', 'IMPT. FACTOR', 'COMMENTS'] if csv.read.size == 0
      csv << [
               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

And this is where /mail_to is called in my JS:

            // 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');
                   });       
            });

I'm seeing this error in my Sinatra app when submitting the form (this wasn't happening before adding the csv writing part):

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'

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..

Was it helpful?

Solution

Remember that every HTTP request must have a reply, which is the return value of your method. In this case, your are implicitly returning the CSV file handler, and Sinatra probably tries to read from it to generate the contents of the reply. Except that you implicitly closed the file handler by using it in a block. So what you need to do is to add a line to return something meaningful (at least, usable), for instance:

post '/mail_to' do
    Pony.mail :subject => " New Inquiry Received",
              :body    => erb(:email),
              :to => params["Email Address"]

    CSV.open('registrations.csv', 'a+') do |csv|
        # do your stuff
    end

    # you can just return a string
    'ok'
end

Also, there's a small copy/paste error in your code at the end of the :to of Pony.mail I guess, the trailing ',.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top