1

my environment is ruby1.9.3+rails3.2.8+passenger.

error: You need to supply at least one validation

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   

my controller is

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 %>

i think it has matter with rails_tiny_mce. before i rails plugin install it, validates is ok under my write. but after i install rails_tiny_mce, the error is display.

my site is http://42.121.5.68/admin/posts.

when i update model to

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

error is Unknown validator: 'OnValidator'

4

2 回答 2

1

这是错误的:

:length => 10..200

它应该是:

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

请参阅Rails 指南

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

validates :content, :title, :length => { :in => 10..200 }
于 2012-11-02T03:38:34.103 回答
1

你应该这样写validates instend of validate

于 2013-08-02T13:32:32.937 回答