5

특정 조건이 충족되지 않으면 다른 HTML 페이지로 리디렉션하려고하는 컨트롤러 요청에 자바 스크립트 요청을 보냈습니다. 몇 가지 질문을 검토 한 결과 다음과 같이 생각해 봤습니다.javascript 요청에서 html로 리다이렉트가 작동하지 않음 (레일즈)

def twittertweet 
    if current_user && current_user.twitter_token 
    .... 
    else 
    flash[:notice] = "You must be registered with Twitter to do that." 
    respond_to do |format| 
     format.js { render(:update) { |page| page.redirect_to authentications_url } } 
    end 
    end 
end 

나는 authentications_url로 redirect하려고합니다.

당신이 볼 수 있듯이
Started POST "/twittertweet.json" for 127.0.0.1 at 2012-04-11 13:38:27 -0400 
    Processing by ItemsController#twittertweet as */* 
    Parameters: {"id"=>"22"} 
    Category Load (0.3ms) SELECT "categories".* FROM "categories" 
Rendered items/update.html.erb within layouts/application (0.0ms) 
Completed 200 OK in 112ms (Views: 79.9ms | ActiveRecord: 5.7ms) 

, 그냥 심지어 authentications_url를 redirect_to 시도 아니에요 : 그러나,이 일어나는 것이다. 그러나 나는 "else"문장에 도달했다는 것을 안다. (필자가 찾을 수 있도록 문장을 넣었다.)

이 문제를 해결할 수있는 방법이 있습니까?

업데이트 DanS의 솔루션을 시도하면 MissingTemplateError가 발생합니다. 아직 항목 번호 업데이트로 리디렉션하려고 :

ActionView::MissingTemplate (Missing template items/update, application/update with {:handlers=>[:erb, :builder, :coffee], :formats=>[:json], :locale=>[:en, :en]}. Searched in: 
    * "/Users/dylandrop/Documents/givespend/app/views" 
    * "/Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/devise-2.0.4/app/views" 
):  
+0

렌더링 (: 업데이트)이 필요합니까? 'format.js {redirect_to authentications_url}' – DanS

+0

@ DanS 만약 내가 이렇게하면 "GET"/ authentications "시작됨 127.0.0.1에 대한 2012-04-11 13:57:03 -0400 AuthenticationsController # index as */* "등 ... 실제로 페이지를로드하지 않습니다. – varatis

+0

@ DanS 다른 말로하면"authentications/index.html.erb "라고 렌더링되었지만 실제로 아무 것도 수행하지 않습니다. – varatis

답변

12

난 당신이 아약스를 통해 twittertweet를 호출 같아요. 그런 다음 javasctipt 코드를 사용하여 응답하십시오.

+0

아, 고맙습니다. – varatis

+0

고맙습니다. 이 대답은 나를 도왔다. –

관련 문제