3

所以我試圖讓client_side_validations寶石的工作,當我標籤我的電子郵件領域,我得到這個錯誤:client_side_validations「不能讀取屬性‘用戶[郵件]’未定義」

Uncaught TypeError: Cannot read property 'user[email]' of undefined 

這是我的表單輔助函數:

<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource), :validate => true) do |f| %> 
     <%= f.email_field :email, :id => "form-email", :placeholder => "[email protected]", :input_html => {:autofocus => true}, :validate => true %>     
     <%= f.submit :label => "Submit", :value => "Sign Me Up!" %> 
     <div class="ajax-response"><%= devise_error_messages! %></div> 
<% end %> 

生成此HTML:

<form accept-charset="UTF-8" action="/users" class="new_user" data-validate="true" id="new_user" method="post" novalidate="novalidate"> 
    <input name="utf8" type="hidden" value="&#x2713;" /> 
    <input name="authenticity_token" type="hidden" value="9JdqaiushdauhsdioauhdoiuhNLSAVCGrc+PLJAQ=" /> 
    <input data-validate="true" id="form-email" input_html="{:autofocus=&gt;true}" name="user[email]" placeholder="[email protected]" size="30" type="email" /> 
    <input label="Submit" name="commit" type="submit" value="Sign Me Up!" /> 
    <div class="ajax-response"></div> 
</form> 

編輯1:即使我將表單助手從f.email_field :email更改爲f.text_field :email ...我仍然收到此錯誤。

回答

2

所以我找到了解決方案,似乎client_side_validationsdevise正在改變形式的id - 這將打破JS驗證。

因此,解決辦法是強制執行的形式的id,所以我form_for幫手,現在看起來是這樣的:

<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource), :validate => true, :html => { :id => 'user_new' }) do |f| %> 

我發現這個solution here

相關問題