Client_side_validations gem似乎根本不起作用,Rails 3.2.13

时间:2013-06-27 16:47:00

标签: ruby-on-rails ruby-on-rails-3.2 rubygems gem client-side-validation

我已将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然后我的//= require rails.validations文件中需要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。

任何见解都将得到充分肯定。

2 个答案:

答案 0 :(得分:1)

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

gem 'client_side_validations', :github => 'bcardarella/client_side_validations', :branch => '3-2-stable'

答案 1 :(得分:0)

我认为您的设置似乎很好。您在表单中缺少“error_messages”类。我在我的项目中做了同样的验证。我的代码是:

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

将上面的代码放在您的表单中进行自定义。您需要指定显示错误的位置以及方式。这一切都在上面的代码中定义。 如果您需要进一步的帮助,请告诉我。