2016-09-30 2 views
0

레일즈에 컨트롤러의 액션에 일부 매개 변수를 전송하기 위해 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"> 

이것은 내 행동을 유도합니다. 가 나는 전문/sing_in 렌더링지고 있다고 내 터미널에서 코드

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 

이 라인을 가지고있다. 그것이 성공적으로 리디렉션되었다고합니다. 물론이 경로는이 경로 도우미 "new_professional_session_path"에 바인딩됩니다. 그러나 페이지를 변경하지 않습니다. 리다이렉트되었지만 작동하지 않는다고 말합니다.

Sometikes 템플릿이 있지만 템플릿 오류가 발생합니다. 제안 사항이 있으십니까?

편집 : 나는 정상에 이것을 넣으면이 respond_to : HTML이 : 나는 ActionView :: MissingTemplate을 얻을

을 JS - 누락 된 템플릿 등록/생성, 응용 프로그램/{로 만들 : 로케일을 = > : [: ja, : html], : variants => [] : 핸들러 => [: erb, : 빌더, 원시, : 루비, : 커피, : arb, : jbuilder ]}.

+0

컨트롤러 파일 이름을 명확히하십시오. 어디로 향하는 지 알지 못하면서 무슨 일이 일어나는지 파악하기가 어렵습니다. 또한, 애플리케이션이'get' 요청을 만들어서는 안되기 때문에'create'라는 템플릿을 찾지 않아야합니다. – Jeff

+0

아약스 양식의 'subscribeResponseHandler'는 무엇입니까? – 7urkm3n

+0

내 컨트롤러를 구독이라고합니다. 그 안에는 새 상자 액션이 있습니다. 새 템플릿에서 Ajax 호출을 만들고 params를 보내 액션을 만듭니다. 그런 다음 create action에서/professional/sign_in 경로 (new_professional_session_path)로 리디렉션하고 싶습니다. 이것은 sebscribe 응답 처리자입니다. function subscribeResponseHandler (응답) { window.location.href = response.forward; } – Hobowpen

답변

0

비동기 요청을 jQuery (형식 js로 처리)를 통해 제출하므로 컨트롤러에서 리디렉션 할 수 없습니다. 당신은 JSON으로 보내는

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

이 코드 줄은 어디에 배치합니까? – Hobowpen

0

및 컨트롤러는 JSON으로 대답하려고 : 당신이 그러나 할 수있는 일

, 당신의 JS 응답보기에 리디렉션하는 것입니다.

컨트롤러 :

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 r u sure, 컨트롤러가 성공적으로 트리거 되었습니까? ajax를 제출 한 후 yr 서버 콘솔 로그를 게시하십시오. – 7urkm3n

+0

@Hobowpen 은 내 게시물 'window.location.href = 'HTTPS를 업데이트 : // localhost를 : 3000/전문 이제/sign_in'' – 7urkm3n

관련 문제