【问题标题】:Mongoid Revert if fail如果失败则恢复 Mongoid
【发布时间】:2012-10-22 04:19:41
【问题描述】:

我在 mongoid 中有一个验证,如果它在保存错误消息时失败,我想恢复它。

例子

if @thing.update_attributes(params[:thing])
  format.html { redirect_to @thing, notice: 'Thing was successfully updated.' }
  format.json { head :no_content }
else
  #revert the fields that have changed and keep @thing.errors
  format.html { render action: "edit" }
  format.json { render json: @thing.errors, status: :unprocessable_entity }   
end

当我保存文档并重新插入时,它似乎不想分配@thing.errors = errors

谢谢

【问题讨论】:

    标签: ruby-on-rails-3 validation error-handling mongoid


    【解决方案1】:

    您可以使用 Active Model Dirty 模块,请参阅此处http://api.rubyonrails.org/classes/ActiveModel/Dirty.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 2015-06-05
      • 2014-05-22
      • 1970-01-01
      • 2016-05-30
      • 2019-09-25
      • 1970-01-01
      相关资源
      最近更新 更多