2013-08-01 37 views
1

長話短說,即時使用這個手動,因爲即時通訊必須創建純HTML格式。Rails的form_authenticity_token沒有返回任何會話[:_ csrf_token]返回實際的令牌

進出口使用的部分(以HAML)

%input{name: "authenticity_token", type: "hidden", value: form_authenticity_token} 

它輸出一個空白的輸入框:

<input name='authenticity_token' type='hidden'> 

但這正常工作:

%input{name: "authenticity_token", type: "hidden", value: session[:_csrf_token]} 

奇怪的事情是,我從docs得到這個

有沒有人有任何想法發生了什麼?

+0

我認爲 - 但不知道 - 該文檔意味着'form_authenticity_token()'只在控制器訪問。 – MrYoshiji

+0

是的,你是正確的,它在控制器中工作,但不在視圖中。你可以張貼這個答案請。 @MrYoshiji – DickieBoy

回答

4

我認爲您引用的文檔暗示此方法form_authenticity_token()只能在控制器中訪問(類:ActionController :: RequestForgeryProtection)。

您應該直接使用session[:_csrf_token]或者你可以設置一個共享變量:

# in controller 
def my_action 
    @auth_token_form = form_authenticity_token 
end 

# in view 
%input{name: "authenticity_token", type: "hidden", value: @auth_token_form} 
+0

謝謝,我結束了'session [:_ csrf_token]' – DickieBoy