2012-08-14 74 views
0

我有一個彈出式登錄表單,通過jQuery Ajax將錯誤信息顯示在表單主體中。它適用於我的本地環境(PostGresQL,WEBbrick),但不適用於Heroku。在Heroku上,用戶被重定向到一個僅顯示錯誤信息的新頁面,即{「error」:「無效的電子郵件地址:[email protected]」}Ajax + Rails無法在Heroku上工作

頁面上實際上有兩種註冊表單,一個通過jQuery對話框彈出,另一個嵌入在頁面上。謝謝你的幫助。

這裏的控制器: 類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 

這裏的JS(我知道,重複的代碼,我只是想獲得它的工作):

// 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(); 
    } 
} 
+0

您能夠發佈錯誤的堆棧跟蹤?錯誤更多的是與你的環境配置而不是JavaScript代碼。 – BlueFish 2012-08-15 01:36:35

回答

0

你environments/production.rb未正確配置爲發送電子郵件。您需要確保它設置爲將電子郵件作爲您實際擁有的域發送。 RailsHeroku都有關於這個問題的好文章。

+0

實際上,在同一頁面上有兩個幾乎相同的註冊表單,而ajax只能在一個上工作,而不能在另一個上工作。不工作的是在jquery.ui對話框中,但它們是相同的,都在我的本地環境中運行。 – schatzkin 2012-08-17 22:26:56