我添加client-side-validations
到我的Gemfile
并运行bundle install
. 然后跑了rails g client_side_validations:install
它创建了config/initializers/client_side_validations.rb
(我的 JS 文件在哪里?资产管道?)
它应该可以解决这个问题,但它似乎没有。
我去config/initializers/client_side_validations.rb
并取消了以下行的注释:
#ClientSideValidations Initializer
# Uncomment to disable uniqueness validator, possible security issue
# ClientSideValidations::Config.disabled_validators = [:uniqueness]
# Uncomment the following block if you want each input field to have the validation messages attached.
# 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
至:
#ClientSideValidations Initializer
# Uncomment to disable uniqueness validator, possible security issue
# ClientSideValidations::Config.disabled_validators = [:uniqueness]
# Uncomment the following block if you want each input field to have the validation messages attached.
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
我决定运行rails g client_side_validations:copy_assets
,然后我需要 rails.validations ( //= require rails.validations
) 在我的application.js
文件中(之前//= require tree .
)。
当我尝试从表单字段中抽出标签时,不会显示任何内联错误,当我提交表单时,也不会显示任何错误。
我的表单和模型有以下代码:
class User < ActiveRecord::Base
has_secure_password
attr_accessible :email, :password, :password_confirmation
validates_presence_of :email
validates_presence_of :password, :on => :create
validates_length_of :password, :minimum => 6
validates_confirmation_of :password
validates_uniqueness_of :email
end
<h1>Sign Up</h1>
<%= form_for User.new, :validate => true do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.submit "Sign Up" %>
</p>
<% end %>
我希望问题出在我的配置或语法上,而不是 gem。
任何见解将不胜感激。