cocoon icon indicating copy to clipboard operation
cocoon copied to clipboard

Remove form inputs based on parameters.

Open EminenceHC opened this issue 11 years ago • 3 comments

Cocoon is listing previously added relationships in my edit form like it is supposed to but I am wondering what is the correct way to hide inputs conditionally. For example, each cocoon field has task_id. I want cocoon to bypass rendering inputs with task_id of 2. I am thinking I could find the inputs with javascript, and hide the parent divs, but I am wondering if there is a way to do it without javascript?

EminenceHC avatar Apr 06 '15 23:04 EminenceHC

You can do something simple like

= f.fields_for :tasks do |task_form|
    - if task_form.object.task_id != 2
      = render 'task_fields', f: task_form

This will only render the nested fields if the task_id is different from 2.

For clarity: if f is your form-object, f.object will point to the object for which the form is built (so here your nested item)

nathanvda avatar Apr 07 '15 22:04 nathanvda

Thanks Nathan!

EminenceHC avatar Apr 07 '15 22:04 EminenceHC

No problem :+1: :smile:

nathanvda avatar Apr 07 '15 23:04 nathanvda