客户端验证未显示 Gem 'client_side_validations'



我正在遵循 rails cast #263 进行客户端验证,并试图让它们出现在我的帖子的屏幕上。到目前为止,一切似乎都很好,但是当我去发布新帖子时,我收到错误

ArgumentError in Posts#new
wrong number of arguments (3 for 2)
Extracted source (around line #1):
<%= form_for @post, :validate => true, :html => {:multipart => true} do |f| %>

这是我的帖子的副本_form

<%= form_for @post, :validate => true, :html => {:multipart => true} do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
  <%= f.file_field :image %>
  </div>
  <div class="field">
     <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

应用程序.htm.erb 文件

<!DOCTYPE html>
<html>
<head>
  <title>Blog</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "rails.validations", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
  <%= favicon_link_tag 'favicon1.ico' %>
  <%= render 'layouts/nav' %>

</head>
<body>
<%= yield %>
</body>
<footer>
<%= render 'layouts/footer' %>
</footer>
<br>
</html>

发布模型

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :comments
  mount_uploader :image, ImageUploader
  validates :title, presence: true,
                    length: { minimum: 5 }
  validates :content, presence: true,
  length: { minimum: 5 }
end

和我的 client_side_validations.rb 文件

# ClientSideValidations Initializer
# Uncomment to disable uniqueness validator, possible security issue
# ClientSideValidations::Config.disabled_validators = [:uniqueness]
# Uncomment to validate number format with current I18n locale
# ClientSideValidations::Config.number_format_with_locale = true
# Uncomment the following block if you want each input field to have the validation messages attached.
#
# Note: client_side_validation requires the error to be encapsulated within
# <label for="#{instance.send(:tag_id)}" class="message"></label>
#
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吗?

因为如果您使用 Rails 4,那么客户端验证已经过时了。你可以在这里查看

http://railscasts.com/episodes/263-client-side-validations?view=comments

还有如果你去 GitHub 页面

https://github.com/bcardarella/client_side_validations

你可以看到它说它不再维护。

或者,您可以尝试使用此分支中的 CSV gem,方法是将 Gemfile 中的 CSV 行替换为此

gem 'client_side_validations', GitHub: "bcardarella/client_side_validations", :branch => "4-0-beta"

不确定这是否有效。既然过时了。

或者你可以看看这个宝石,

https://github.com/kalkov/rails4_client_side_validations

它只是客户端验证的修改版本

最新更新