steps.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. class CustomWizard::StepsController < ApplicationController
  2. before_action :ensure_logged_in
  3. def update
  4. params.require(:step_id)
  5. params.require(:wizard_id)
  6. field_ids = CustomWizard::Wizard.field_ids(params[:wizard_id], params[:step_id])
  7. permitted = params.permit(:wizard_id, :step_id)
  8. if params[:fields]
  9. permitted[:fields] = params[:fields].select { |k, v| field_ids.include? k }
  10. permitted.permit!
  11. end
  12. wizard = CustomWizard::Builder.new(current_user, permitted[:wizard_id].underscore).build
  13. updater = wizard.create_updater(permitted[:step_id], permitted[:fields])
  14. updater.update
  15. if updater.success?
  16. result = success_json
  17. result.merge!(updater.result) if updater.result
  18. result[:refresh_required] = true if updater.refresh_required?
  19. render json: result
  20. else
  21. errors = []
  22. updater.errors.messages.each do |field, msg|
  23. errors << { field: field, description: msg.join }
  24. end
  25. render json: { errors: errors }, status: 422
  26. end
  27. end
  28. end