nested attributes ãªã¬ã³ã¼ããåé¤ãããå ´åãaccepts_nested_attributes_for
ã« allow_destroy: true
ãªãã·ã§ã³ã渡ãã¨åé¤å¯è½ã«ãªãã¾ããåé¤ããã«ã¯ã対象ã¨ãªã対象㫠{ _destroy: 1 }
ã®ãããªãã©ã¡ã¼ã¿ã渡ãã¾ãã
ãããè¸ã¾ãã¦ç´ ç´ã«ãã©ã¼ã ãä½ããã¨ããã¨ãåé¤ç¨ã®ãã§ãã¯ããã¯ã¹ãã¤ããäºã«ãªãã§ããããããã次ã®ãããªãã©ã¼ã ã«ãã§ãã¯ããã¯ã¹ãã¤ããã¨ãã¦ã¼ã¶ã«ã¨ã£ã¦ãããã¥ããUIã«ãªã£ã¦ãã¾ãã¾ããåç´ã«ããã¹ããã£ã¼ã«ãã空ã«ãã¦æ´æ°ãããã¬ã³ã¼ããåé¤ããã¦æ¬²ããã
ããã§æ¬¡ã®ããã«ãã¾ãã
class User < ActiveRecord::Base accepts_nested_attributes_for :family_members, reject_if: :reject_family_member, allow_destroy: true def reject_family_member(attributes) exists = attributes[:id].present? empty = attributes[:email].blank? attributes.merge!(_destroy: 1) if exists && empty !exists && empty end end
reject_if
ã®æç¶ãã®ä¸ã§ãã¬ã³ã¼ãã¨ãã¦ä¿åæ¸ã¿ã§ãã¤ã¡ã¼ã«ã¢ãã¬ã¹ã空ã®ãã®ã« { _destroy: 1 }
ã追å ãã¦ãã¾ããããã§ãã§ãã¯ããã¯ã¹ãªã㧠nested attributes ãªã¬ã³ã¼ããåé¤ãããã¨ãåºæ¥ã¾ããã
åè
ruby on rails - Destroy on blank nested attribute - Stack Overflow