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

时间:2012-11-02 03:27:27

标签: ruby-on-rails

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

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

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'

2 个答案:

答案 0 :(得分:1)

这是错误的:

:length => 10..200

它应该是:

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

请参阅Rails Guides

除了:length => { :in => 10..200 }已经确保该字段不为空,因此您可以摆脱:presence => true

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

答案 1 :(得分:1)

你应该像这样写验证而不是验证