2016-06-17 35 views
0

我剛接觸rails並遵循Micheal Hartl的教程。在他的教程中,他使用'form_for'標籤在他的應用中創建表單。但是,我喜歡css框架,因此決定使用Zurb Foundation來手動創建HTML表單。表單是在模式內部創建的。這裏是表單代碼:爲什麼我的ruby on rails應用程序不在我的部分html頁面中渲染rails代碼?

<div class="reveal" id="StudentLoginModal" data-reveal 
    data-animation-in="hinge-in-from-top" data-animation-out="hinge-out-from-bottom"> 

    <form data-abide novalidate action="/login" method="POST"> 
    <%= form_authenticity_token %> 
    <div data-abide-error class="alert callout" style="display: none;"> 
     <p><i class="fi-alert"></i> There are some errors in your form.</p> 
    </div> 
    <div class="row"> 

     <label style="font-family: sans-serif; 
     font-size: 1.15em; 
     font-weight: bold;">UserName 
     <input name="session[name]" type="text" placeholder="Your Exam Roll.no" required pattern="number" 
     aria-describedby= "exampleHelpText"> 
     <span class="form-error"> 
      Yo, you had better fill this out, it's required. 
     </span> 
     <p class="help-text" id="exampleHelpText">Enter your username.</p> 
     </label> 

     <label style="font-family: sans-serif; 
     font-size: 1.15em; 
     font-weight: bold;">Password 
     <input type="password" name="session[password]" id="password" placeholder="yeti4preZ" aria-describedby= "exampleHelpText1" required > 
     <span class="form-error"> 
     I'm required! 
     </span> 
     <p class="help-text" id="exampleHelpText1">Enter a password please.</p> 
    </label> 
    <input type="submit" class="button" value="Submit"> 
    </div> 
</form> 

    <button class="close-button" data-close aria-label="Close reveal" type="button"> 
     <span aria-hidden="true">&times;</span> 
    </button> 

</div> 

但問題是,Rails已經CSRF保護,它不會允許用戶提交表單沒有真實性令牌。我試圖使用<%= form_authenticity_token%>檢索值,但它沒有向窗體添加任何值。但是,該標籤顯示其他頁面中令牌的值。有任何想法嗎?

謝謝:)

編輯1:我認爲這個問題是我使用的渲染登錄表單的模式。

+0

你可以把'skip_before_filter:verify_authenticity_token'在你的控制器。 –

+0

但是,這將關閉CSRF。這不是建議嗎? – Prajjwal

回答

0

使用
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
代替<%= form_authenticity_token %>

+0

試過,但也沒有工作.. – Prajjwal

+0

您是否使用ajax請求呈現此表單? –

+0

那麼點擊登錄按鈕是由基礎庫處理的,所以我不知道他們是否使用AJAX,但點擊顯示模式,所以我認爲它是Ajax。 – Prajjwal

0

在控制器

def my_action 
    @auth_token_form = form_authenticity_token 
end 

鑑於

<input type="hidden" name="authenticity_token" value="<%=form_authenticity_token %>"> 
+0

不工作。我認爲它沒有顯示代碼,因爲我用來渲染表單的模態。 – Prajjwal

+0

'form_authenticity_token()'只能在控制器(類:ActionController :: RequestForgeryProtection)中訪問。 –

相關問題