0

我添加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。

任何见解将不胜感激。

4

2 回答 2

1

3-2-stable在您的 Gemfile 上使用,然后运行 ​​bundle install

gem 'client_side_validations', :github => 'bcardarella/client_side_validations', :branch => '3-2-stable'
于 2013-06-27T17:01:48.363 回答
0

我认为您的设置看起来不错。您的表单中缺少“error_messages”类。我在我的项目中做了同样的验证。我的代码是:

<%= f.error_messages :header_message => "oops! you missed something",
  :message => "",
  :header_tag => :h3, :class => "background-color:#d24d33; color:#ffffff" %>

将上面带有自定义的代码放入您的表单中。您需要指定在哪里显示错误以及如何显示错误。这一切都在上面的代码中定义。如果您需要进一步的帮助,请告诉我。

于 2013-06-27T16:53:18.693 回答