0

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

1 Answer 1

2

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

2
  • Thanks very much for this! Worked perfectly. Out of curiosity, how come I didn't receive this error before adding the CSV part? in other words, wasn't my /mail_to route returning a Pony object before, shouldn't I have received a similar error?
    – bhaity
    Commented Jan 13, 2014 at 17:31
  • I don't really have an answer for that. If it works with only Pony.mail, it means that Sinatra, whatever it does internally, can use the return value of Pony.mail (whatever it is, too, can't find it in the docs) to generate a reply message.
    – To마SE
    Commented Jan 15, 2014 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.