【问题标题】:error You need to supply at least one validation错误您需要提供至少一项验证
【发布时间】:2012-11-02 03:27:27
【问题描述】:

我的环境是ruby1.9.3+rails3.2.8+passenger。

错误:您需要提供至少一项验证

class Post < ActiveRecord::Base
  attr_accessible :content, :title, :url, :tags_attributes, :published, :category_id

  has_many :comments, :dependent => :destroy
  has_many :tags, :dependent => :destroy

  belongs_to :category

  validates :content, :presence => true
  validates :title, :presence => true
  validates :url,   :presence => true
  validates :tags_attributes, :presence => true
  validates :published, :presence => true
  validates :category_id, :presence => true

  accepts_nested_attributes_for :tags, :allow_destroy => true,
         :reject_if => proc { |attrs| attrs.all? { |k,v| v.blank? } }


  scope :published, where(:published => true)
end   

我的控制器是

class Admin::PostsController < Admin::ApplicationController

  uses_tiny_mce(:options => AppConfig.default_mce_options, :only => [:new, :edit])

  def index
    page = params[:page]
    if page == nil
      page = 1
    end
end

index.html.erb

<h2>Post list</h2>
<table>
  <tr>
    <th>Title</th>
    <th>Category</th>
    <th>Created</th>
    <th>Updated</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
  <% @posts.each do |post| %>
  <tr>
    <td><%= post.title %></td>
    <td><%= post.category.title %></td>
    <td><%= post.created_at.localtime.to_s(:db) %></td>
    <td><%= post.updated_at.localtime.to_s(:db) %></td>
    <td><%= link_to 'Edit', edit_admin_post_path(post) %></td>
    <td><%= link_to 'Delete',[:admin, post], :method => :delete, :confirm => 'are you sure?' %></td>
    <td></td>
  </tr>
  <% end %>
</table>

<br/>
<%= will_paginate @posts %>
<br/>
<%= link_to 'New Post', new_admin_post_path %>

我认为它与 rails_tiny_mce 有关系。 在我安装 Rails 插件之前,在我的编写下验证是可以的。 但是在我安装 rails_tiny_mce 之后,就会显示错误。

我的网站是http://42.121.5.68/admin/posts.

当我将模型更新为

validates [:content, :title], :on => :save, :allow_blank => false,
            :presence => true, :length => { :in => 10..200 }

错误是未知的验证器:'OnValidator'

【问题讨论】:

  • 信息不够。您正在执行什么操作来查看此错误?它是出现在浏览器中还是日志文件中?
  • posts/index,当我使用验证替换验证时,错误消失,页面正常。但验证不起作用。
  • ` 验证(attributes)默认值 = attributes.extract_options! validations = defaults.slice!(_validates_default_keys) raise ArgumentError, "You need at least one attribute" if attributes.empty?引发 ArgumentError,“您需要提供至少一个验证”如果 validations.empty? `
  • 抱歉,我已将它们添加到我的问题中。
  • 我查看了您的网站,错误在 app/models/post.rb,而不是在 comment.rb。请向我们展示您的Post 型号,而不是Comment。也可以在您的 Post 模型中尝试我的建议。

标签: ruby-on-rails


【解决方案1】:

这是错误的:

:length => 10..200

应该是:

:length => { :in => 10..200 }

Rails Guides

除了:length =&gt; { :in =&gt; 10..200 }已经确保该字段不为空,所以你还不如去掉:presence =&gt; true

validates :content, :title, :length => { :in => 10..200 }

【讨论】:

  • 验证 :title, :length => { :in => 10...200 }
  • 你用了两个点,对吧? :in =&gt; 10..200,不是三个。然后不知道怎么了。请出示相关控制器并查看代码。
【解决方案2】:

你应该这样写 validates 支持 validate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    相关资源
    最近更新 更多