Pregunta

I have dynamic form with conditional attributes.

E.g. if radio button "condition1" is checked, when "price" field hides dynamically via jQuery, and "percentage" field appears.

Let's say I created record with "condition1" radio box and set "percentage" value. Then, I decided to update record, and disabled "condition1", so percentage field hides, and I set new value for "price".

The problem is, that if I disable "condition1", "percentage" parameter is still present, and what I need is either "price" or "percentage" parameter at a time. I can removeAttribute() with jQuery on click, but this is bad user experience, because in some fields I have multiline textareas and I don't want to force users to write all again if they accidentally clicked on radio button.

I tried playing with ActiveModel::Dirty with no luck

in edit action of object's controller

   if @object.condition_changed? && @object.condition_was == "condition1"
      @object.attribute(:percentage, nil)
    end
¿Fue útil?

Solución

you can run a after update callback like

after_update: set_nil_to_unrequired_field 

def set_nil_to_unrequired_field 
 update_column('your field ', nil) if "other field is present"
end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top