我目前使用ClientSideValidations gem,並在呈現使用ajax的部分時嘗試驗證使用上述gem呈現的部分內的地址。輸入模型中指定的錯誤組合時沒有任何反應。在ajax中使用ClientSideValidations gem呈現部分
如果直接瀏覽地址表單並嘗試驗證,則一切正常,就像指定一樣。
有關如何使驗證在部分內部工作的任何提示或想法?
謝謝!
編輯:在JS控制檯中沒有錯誤,例如輸入一個太短的zipcode(在5位數字的模型中指定)時就沒有任何反應。順便說一句,我用haml的意見。
所以在我的視圖的代碼:
= link_to "Shipping", addresses_path, :remote => true
相應的控制器addresses_controller.rb
respond_to do |format|
...
format.js {render :layout => false}
...
end
對應index.js.erb的
$("#ajax_content").html("<%= escape_javascript(render :partial =>
"partialXY") %>");
和相應的局部
= form_for @address, :validate => true, :id => "address_form", :remote => true do |f|
- if @address.errors.any?
#error_explanation
%h2
= pluralize(@address.errors.count, "error")
prohibited this user from being saved:
%ul
- @address.errors.full_messages.each do |msg|
%li
=msg
%ul
%li
= f.label :type
= f.select :address_type, [['Billing Address', 'billing'],['Shipping Address',
'shipping']], :class => "address_selection"
%li
= f.label :gender
= f.select :gender, [['Male', 'male'],['Female', 'female']], :class => "text_field"
%li
= f.label :last_name
= f.text_field :last_name, :id => "last_name", :class => "text_field"
%li
= f.label :first_name
= f.text_field :first_name, :class => "text_field"
%li
= f.label :company_name
= f.text_field :company_name, :class => "text_field"
%li
= f.label :street
= f.text_field :street, :class => "text_field"
%li
= f.label :number
= f.text_field :number, :class => "text_field"
%li
= f.label :zipcode
= f.text_field :zipcode, :class => "text_field"
%li
= f.label :place
= f.text_field :place, :class => "text_field"
%li
= f.label :phone_no
= f.text_field :phone_no, :class => "text_field"
%li
= f.label :country
= f.text_field :country, :class => "text_field"
%li
= f.label :email
= f.text_field :email, :class => "text_field"
%li
= f.submit
所以就像我說的一樣驗證郵政編碼等有趣的是,所呈現的部分輸入時沒有任何反應,如果你看看下面的,自動生成的軌道視圖編輯地址,驗證工作得很好。
軌生成視圖編輯地址
=render 'partialXY'
我一直在這個問題上很長一段時間,並且對如何解決這一問題絲毫沒有線索。我敢肯定它與ajax有關,因爲使用驗證時,渲染軌道生成的部分作品很好。
非常感謝!菲爾
什麼不行?你的日誌或JS控制檯中是否有錯誤?你能告訴我們你的代碼嗎? –
@Jordan添加了代碼!讓我知道什麼時候失蹤了!菲爾 – user966041