2016-09-30 88 views
0

我有一個模板在Rails中,我使用ajax提交一些參數在我的控制器中的操作。Rails動作顯示它重定向,但它不

<script type="text/javascript"> 
     $(document).ready(function() { 
    $("#subscribe-form").on('submit', function(e) { 
       e.preventDefault(); 
       var form = $('#subscribe-form') 
       if (!$(form).valid()) { 
        return false; 
       } 
       showProcessing(); 
       client.tokenizeCard({ 
        number: $('input[card-info=number]').val(), 
        expirationDate: $('select[card-info=expiry_month]').val()+ "/" + $('select[card-info=expiry_year]').val(), 
        cvv : $('input[card-info=cvv]').val() 
    },function (err, nonce) { 
        console.log(nonce); 

        hideProcessing() 
        if(err){ 
        $(".alert-danger").show().text("Couldn't process your card"); 
        hideProcessing(); 
        return; 
        } 
        console.log(nonce); 

        if ($("input[name='braintreeToken']").length == 1) { 
         $("input[name='braintreeToken']").val(nonce); 
        } else { 
         form.append("<input type='hidden' name='braintreeToken' value='" + nonce + "' />"); 
        } 
        console.log(nonce); 

        var options = { 
         success: subscribeResponseHandler, 
         complete: hideProcessing, 
         contentType: 'application/x-www-form-urlencoded; charset=UTF-8', 
         dataType: 'json', 
         //url: 'http/localhost:3000/subscription/create' 
        }; 
        $(form).ajaxSubmit(options); 
    }); 
       return false; 
      }); 
     }); 
    </script>` 

這就是我在我的「訂閱」控制器中將參數提交給我的「動作」方法的方式。

`     <form action="/subscription/create" method="post" id="subscribe-form"> 

這進入我的創建行動。 在那裏,我有這行代碼

if true then 
    File.open("letssee.txt", 'w') { |file| file.write("eftase edw")} 

    customer.update(has_plan: true) 
    respond_to do |format| 

     format.html { redirect_to(new_professional_session_path, :notice => 'Feedback was successfully updated.') } 
     format.js 
    end 

    #this also does not work 
    #redirect_to(new_professional_session_path, format: :html) 
    return 
else 
     File.open("letssee.txt", 'w') { |file| file.write("den eftase")} 

    redirect_to :action=>'new' 
    return 
end 

在我的終端,它說的是,專業/ sing_in越來越呈現。它說它已成功重定向到那裏。 當然這條路線綁定到這個路徑助手「new_professional_session_path」。 但它不會改變頁面.. 它說它被重定向,但它不..

Sometikes雖然模板存在,我也會得到一個缺少的模板錯誤。 有什麼建議嗎?

編輯: 如果我把這個在上面, 的respond_to:HTML,:JS

我得到 ::的ActionView MissingTemplate - 缺少模板訂閱/創建,應用程序/創建{:區域= > [:en],:formats => [:js,:html],:variants => [],:handlers => [:erb,:builder,:raw,:ruby,:coffee,:arb,:jbuilder ]}。

+0

請說明控制器的文件名。不知道你從哪裏轉到哪裏,很難弄清楚發生了什麼。此外,它不應該尋找一個名爲'create'的模板,因爲你的應用程序不應該向該路由發出'get'請求。 – Jeff

+0

whats yr'subscribeResponseHandler'在ajax形式? – 7urkm3n

+0

我的控制器被稱爲訂閱。裏面有新的和箱子的動作。 在新模板中,我進行Ajax調用併發送參數以創建操作。 然後,我想從創建操作重定向到/ professional/sign_in路徑(new_professional_session_path)。 這是sebscriberesponsehandler, 函數subscribeResponseHandler(響應){ window.location.href = response.forward; } – Hobowpen

回答

0

您正通過jQuery提交異步請求(處理類型爲js),因此無法在您的控制器中重定向。

但是你可以做什麼,是你的js響應以重定向:

window.location= '<%= new_professional_session_path %>' 
+0

在哪裏放置這行代碼? – Hobowpen

0

您發送它作爲JSON和控制器試圖回答的JSON。

控制器:

respond_to do |format| 

    format.html { redirect_to(new_professional_session_path, :notice => 'Feedback was successfully updated.') } 

    #add this one here 
    format.json { render status: 200, json: new_professional_session_path } 

    format.js 
end 

$阿賈克斯

var options = { 
    success: subscribeResponseHandler, 
    complete: hideProcessing, 
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8', 
    dataType: 'json', 
    //url: 'http/localhost:3000/subscription/create' 
}; 

# HERE! 
# I dnt know whats exactly callback will be here. 
function subscribeResponseHandler(responseText, statusText) { 
    window.location.href = 'https://localhost:3000/professional/sign_in' 
} 

$(form).ajaxSubmit(options) 
+0

它似乎沒有工作.. – Hobowpen

+0

@Hobowpen你確定,你已經成功觸發控制器?提交ajax後發佈yr服務器控制檯日誌。 – 7urkm3n

+0

@Hobowpen 已更新我的帖子 'window.location.href ='https:// localhost:3000/professional/sign_in'' – 7urkm3n

相關問題