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

时间:2013-11-16 01:32:48

标签: ruby-on-rails gem

我正在关注使用rails#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 %>

application.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

这里的任何人都知道问题是什么,或者为什么会发生这种情况?我错过了或拼写错了吗?

1 个答案:

答案 0 :(得分:0)

您能否告诉您使用的是哪个版本的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 =&gt; “4-0-β”

即使这会起作用也不确定。自其过时。

或者你可以检查这个宝石,

https://github.com/kalkov/rails4_client_side_validations

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