2012-11-02 45 views
1

我的环境是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_tiny_mce后,显示错误。

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

当我更新模型

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

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

+1

这没有足够的信息。你正在执行什么操作来查看此错误?它是否显示在浏览器或日志文件中? – noodl

+0

在浏览器中。 – andyshi

+0

职位/索引,当我使用验证替换验证,错误是不合格的,页面是好的。但验证不起作用。 – andyshi

回答

1

这是错误的:

:length => 10..200 

它应该是:

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

请参阅Rails Guides

除了:length => { :in => 10..200 }已经可以确保字段不为空,所以你还不如干掉:presence => true

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

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

+0

错误仍然存​​在。 – andyshi

+1

你用了两个点,对吧? ':in => 10..200',而不是三个。然后我不知道什么是错的。请显示相关的控制器和查看代码。 – Mischa

1

你应该写这样的验证instend的验证

相关问题