简体   繁体   中英

Ajax + Rails not working on Heroku

I have a pop-up sign-in form that has the error messages display in the body of the form via jQuery Ajax. It works fine on my local environment (PostGresQL, WEBbrick) but not on Heroku. On Heroku, the user is redirected to a new page which displays just the error message ie, {"error":"Invalid Email Address: testing@test.com"}

There are actually two signup forms on the page, one that pops up via jQuery Dialog, and the other is embedded on the page. Thanks for any help.

Here's the controller: class MailingListController < ApplicationController

 respond_to :json

 def create
   gb = Gibbon.new(Settings.mailchimp.api_key)
   res = gb.list_subscribe({:id => Settings.mailchimp.list_id, :email_address => params[:email]})
   if res == true
     render(:json => {:body => "okay"})
   else
     render(:json => {:error => res["error"]})
   end
 rescue
   render(:json => {:error => "Fatal Error"})
  end
end

Here's the js (I know, duplicate code, I'm just trying to get it to work):

  // Mailing List Watcher
 var mailingList2 = $("#mailing-list2");
 if ( mailingList2.length ) {
   mailingList2
     .live("completed", function(e){
     })
     .live("success", function(e){
       var that = this;
       $.cookie("mailingListSubmitted", "true", {expires: 7});
       if(mailingList2.find("#mailing-list2 #status2").length == 0){
         mailingList2
           .find("form input[type='text']")
           .after($("<div></div>").attr({id : "status2"}))
       }
       mailingList2
         .find("form input[type='text']")
           .attr("disabled", true)
           .fadeOut(5000);
       $("#mailing-list2 #status2")
         .text("Email submitted successfully!")
         .effect("highlight", { }, 1000);
     })
     .live("failure", function(e, error){
      if(mailingList2.find("#mailing-list2 #status2").length == 0){
         mailingList2
           .find("form input[type='text']")
           .after($("<div></div>").attr({id : "status2"}))
       }
       $("#mailing-list2 #status2")
         .text(error)
         .effect("highlight", {}, 1000);
     })
     .live("submittal", function(e, emailAddress){
       if ( emailAddress == "" || emailAddress == null ) {
         $(this).trigger("failure", ["You need to specify an email address!"])
         return false;
       }
       var token = $.token();
       $.post("/mailing_list", {email: emailAddress, authenticity_token: token}, function(response, status, xhr){
         if(response.error){
           $(mailingList2).trigger("failure", ["An error occurred: " + response.error]);
         } else {
           $(mailingList2).trigger("success");
         }
       }, "json")
       .error(function(){
         $(mailingList2).trigger("failure", ["An error occurred. Please try again in a few minutes."]);
       });
     });


   mailingList2.find("form").submit(function(){
     emailAddress = mailingList2.find("input[name='email']").val();
     $(mailingList2).trigger("submittal", [emailAddress]);
     return false;
   });

   var mlSetting = $.cookie("mailingListSubmitted");
   if ( mlSetting == "true" ) {
     mailingList2.remove();
   }
 }

 // Mailing List Watcher
 var mailingList = $("#mailing-list");
 if ( mailingList.length ) {
   mailingList
     .live("completed", function(e){
     })
     .live("success", function(e){
       var that = this;
       $.cookie("mailingListSubmitted", "true", {expires: 7});
       if(mailingList.find("#status").length == 0){
         mailingList
           .find("form input[type='text']")
           .after($("<div></div>").attr({id : "status"}))
       }
       mailingList
         .find("form input[type='text']")
           .attr("disabled", true)
           .fadeOut(5000);
       $("#status")
         .text("Email submitted successfully!")
         .effect("highlight", { }, 1000);
     })
     .live("failure", function(e, error){
      if(mailingList.find("#status").length == 0){
         mailingList
           .find("form input[type='text']")
           .after($("<div></div>").attr({id : "status"}))
       }
       $("#status")
         .text(error)
         .effect("highlight", {}, 1000);
     })
     .live("submittal", function(e, emailAddress){
       if ( emailAddress == "" || emailAddress == null ) {
         $(this).trigger("failure", ["You need to specify an email address!"])
         return false;
       }
       var token = $.token();
       $.post("/mailing_list", {email: emailAddress, authenticity_token: token}, function(response, status, xhr){
         if(response.error){
           $(mailingList).trigger("failure", ["An error occurred: " + response.error]);
         } else {
           $(mailingList).trigger("success");
         }
       }, "json")
       .error(function(){
         $(mailingList).trigger("failure", ["An error occurred. Please try again in a few minutes."]);
       });
     });


   mailingList.find("form").submit(function(){
     emailAddress = mailingList.find("input[name='email']").val();
     $(mailingList).trigger("submittal", [emailAddress]);
     return false;
   });

   var mlSetting = $.cookie("mailingListSubmitted");
   if ( mlSetting == "true" ) {
     mailingList.remove();
   }
}

Your environments/production.rb is not configured properly to send email. You need to make sure that it is set up to send email as a domain you actually own. Rails and Heroku both have good articles on the subject.

答案就在这里,尽管我以前读过这篇文章,但还是做了一些尝试才能使它起作用: https : //devcenter.heroku.com/articles/pgbackups#exporting-via-a-backup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM