代码之家  ›  专栏  ›  技术社区  ›  Damien

使用simple_form的RoR:是否自动为“空”属性指定nil?

  •  0
  • Damien  · 技术社区  · 14 年前

    当一个空字段通过表单提交时,该属性就会变成“”,就像在空白中一样,但我认为它应该是nil?

    如果属性更新为空,是否可以将其保持为零?

    我确实找到了这块宝石:

    https://github.com/grosser/clear_empty_attributes

    但我不明白为什么到目前为止还没有解决这个问题(那颗宝石已经3年了),也许我错过了什么。

    1 回复  |  直到 14 年前
        1
  •  1
  •   cthulhu    14 年前

    关于:

    class Foo < ActiveRecord::Base
    
      before_update :clear_empty_attrs
    
      def clear_empty_attrs
        changes.each { |k, v| write_attribute(k, nil) if v.last.blank? }
      end
    end