Module: Formbuilder::Concerns::FormsController

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/formbuilder/concerns/forms_controller.rb

Instance Method Summary collapse

Instance Method Details

#updateObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/formbuilder/concerns/forms_controller.rb', line 12

def update
  existing_response_field_ids = []
  cids = {}

  (params[:fields] || []).each_with_index do |field_params, i|
    response_field = get_or_build_field(field_params[:id])
    response_field.update_attributes(transformed_field_params(field_params, i))
    cids[response_field.id] = field_params[:cid]
    existing_response_field_ids.push response_field.id
  end

  # destroy fields that no longer exist
  @form.response_fields.each do |response_field|
    response_field.destroy unless response_field.id.in?(existing_response_field_ids)
  end

  @form.response_fields.reload

  @form.response_fields.each do |rf|
    rf.cid = cids[rf.id]
  end

  render json: @form.response_fields_json
end