0

我正在开发一个带有 Mongoid 后端的 rails 3.2.9 项目。我正在尝试创建帖子,如果帖子缺少标题和内容,则模型应该无法正确保存它。当一个对象被修复并通过验证时,我保存它并现在创建一个新帖子。如果我尝试用丢失的项目保存这个,错误计数似乎只是附加前一个,即使它们是两个不同的对象。

我正在使用来自dynamic_form的 error_messages 助手来显示我的错误。有任何想法吗?

这是一个示例错误消息:

152 错误禁止保存该帖子 以下字段存在问题: 标题不能为空 标题不能为空 标题不能为空 标题不能为空 标题不能为空

这份名单还在继续。代码只是基本的表单代码:

= form_for @post,:as => :post, :url => post_path(:id=>@post.id), :method => :put do |f|
  =f.hidden_field :is_question
  #content        
    .title-page
      %h1 
        New Post        
    = f.error_messages

在我的模型中:

validates_presence_of :title
validates_presence_of :content

和我的控制器方法:

def publish      
  @post = Post.first(conditions:{_id:params[:post_id]}) 
  @post.assign_attributes(params[:post])

  @post.published=true  

  if @post.save           
    redirect_to "/"
  else
    @video = Video.new    
    render action: "new"
  end    

end
4

0 回答 0