ActiveRecord - replace model validation error with warning

后端 未结 3 1025
滥情空心
滥情空心 2021-02-04 12:51

I want to be able to replace a field error with a warning when saving/updating a model in rails. Basically I want to just write a wrapper around the validation methods that\'ll

3条回答
  •  走了就别回头了
    2021-02-04 13:39

    I made my own gem to solve the problem for Rails 4.1+: https://github.com/s12chung/active_warnings

    class BasicModel
      include ActiveWarnings
    
      attr_accessor :name
      def initialize(name); @name = name; end
    
      warnings do
        validates :name, absence: true
      end
    end
    
    model = BasicModel.new("some_name")
    model.safe? # .invalid? equivalent, but for warnings
    model.warnings # .errors equivalent
    

提交回复
热议问题