我似乎无法获得我想要的自定义错误显示,我已取消注释 config/initializers/client_side_validations.rb 中的文本并按照文档中的描述创建了 rails.validations.actionView.js 但是在调试表单时我似乎无法使用自定义文件(我还在 rails.validations.js 文件上放置了一些断点,它没有在那里添加或删除,所以这可能是问题所在。所有文件都已加载通过资产管道和据我所知。
gem 'rails', '3.2.8'
gem 'formtastic'
gem 'client_side_validations' , '3.2.0'
gem 'client_side_validations-formtastic'
动作视图.js
window.ClientSideValidations.formBuilders['ActionView::Helpers::FormBuilder'] = {
add: function(element, settings, message) {
// custom add code here
console.log('added')
},
remove: function(element, settings) {
console.log('removed');}}
_form.html.erb
<%= semantic_form_for @sponsorship, :validate => true do |f| %>
<%= f.inputs do %>
<%= f.input :title, :hint => "Give us a title" %>
<%= f.input :bio %>
<%= f.input :blurb %>
<%= f.input :category_id, :as => :select, :collection => Category.all, :label_method => :title %>
<%= f.input :event_id, :as => :select, :collection => Event.all, :label_method => :title %>
<%= f.input :date_end, :start_year => 2012, :as => :date_picker %>
<%= f.input :funds_needed %>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :agree_to_terms %>
<%= f.validate %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
头部中的 HTML 输出
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/rails.validations.js?body=1" type="text/javascript"></script>
<script src="/assets/rails.validations.formtastic.js?body=1" type="text/javascript"></script>
<script src="/assets/rails.validations.customValidators.js?body=1" type="text/javascript"></script>
<script src="/assets/rails.validations.actionView.js?body=1" type="text/javascript"></script>
client_side_validation.rb
require 'client_side_validations/simple_form' if defined?(::SimpleForm)
require 'client_side_validations/formtastic' if defined?(::Formtastic)
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{<div class="toto"><div class="field_with_errors">#{html_tag}</div><label for="#{instance.send(:tag_id)}" class="message" style="float:right;">#{instance.error_message.first}</label></div>}.html_safe
else
%{<div class="toto">#{html_tag}</div>}.html_safe
end
end
我是rails新手,所以我可能在某个地方失态了。有任何问题请告诉我!谢谢阅读。