我有带有条件属性的动态表单。
例如,如果选中了单选按钮“ condition1”,则当“价格”字段通过jQuery动态隐藏时,会出现“百分比”字段。
假设我使用“ condition1”单选框创建了记录,并设置了“ percentage”值。然后,我决定更新记录,并禁用“ condition1”,因此隐藏了百分比字段,并为“ price”设置了新值。
问题是,如果我禁用“ condition1”,则“ percentage”参数仍然存在,而我需要的是一次“ price”或“ percentage”参数。我可以在单击时使用jQuery移除removeAttribute(),但这是糟糕的用户体验,因为在某些字段中,我具有多行文本区域,并且如果用户不小心单击了单选按钮,我不想强迫用户重新编写所有内容。
我试过玩ActiveModel :: Dirty没运气
在对象控制器的编辑动作中
if @object.condition_changed? && @object.condition_was == "condition1"
@object.attribute(:percentage, nil)
end
您可以像这样运行更新后回调
after_update: set_nil_to_unrequired_field
def set_nil_to_unrequired_field
update_column('your field ', nil) if "other field is present"
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句