【发布时间】: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