我真的不知道我做错了什么。我在 gem 的 3-1-stable 分支上。不知道是bug还是我的愚蠢。client_side_validations 只是无法识别我的验证。
这是我的代码:
# model models/fan.rb
class Fan < ActiveRecord::Base
validates_presence_of :email, :name
validates_uniqueness_of :email
end
# initializer initializers/client_side_validations.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
else
%{<div class="field_with_errors">#{html_tag}</div>}.html_safe
end
end
# view fans/new.haml
= form_for @fan, validate: true do |f|
= f.text_field 'name'
= f.email_field 'email'
= f.submit
# application.js
//= require jquery
//= require rails.validations
# gemfile
gem 'aws-sdk'
gem 'aws-s3', :require => 'aws/s3'
gem 'client_side_validations', git: 'https://github.com/bcardarella/client_side_validations.git', branch: '3-1-stable'
gem 'coffee-filter'
gem 'devise'
gem 'draper'
gem 'haml'
gem 'haml-rails'
gem 'heroku'
gem 'jquery-rails'
gem 'paperclip', '~> 3.0'
gem 'paperclip-meta', :git => "git://github.com/dce/paperclip-meta.git"
gem 'rails', '~> 3.2.3'
gem 'thin'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'coffee-rails', '~> 3.2.2'
gem 'sass-rails', '~> 3.2.5'
gem 'uglifier'
end
group :production do
gem 'pg'
end
group :development, :test do
gem 'awesome_print'
gem 'powder'
gem 'sqlite3'
gem 'sprite-factory'
end
我可以提交表格(我不应该这样做)。验证工作(rails 让我返回预期的消息)。我尝试通过扫描 gem 代码来调试它,我发现在每个具有验证的表单之后都插入了一个脚本标签。我的问题是 window[form.id].validators 总是空的。
我使用普通的表单生成器。我尝试剥离所有其他脚本/样式/html,直到我只有表单和所需的 javascript(jquery 和 rails.validations),但它不起作用。
任何帮助是极大的赞赏 :(