@@ -32,17 +32,29 @@ def create
3232 checklist_item : @checklist_item )
3333 pci . completed_at = params [ :completed ] ? Time . zone . now : nil
3434
35- if pci . save
36- Rails . logger . info ( "DBG PersonChecklistItemsController#create: saved pci id=#{ pci . id } completed_at=#{ pci . completed_at } " )
37- # If checklist completed, trigger a hook (implement as ActiveSupport::Notifications for now)
38- notify_if_checklist_complete ( person )
39- render json : { id : pci . id , completed_at : pci . completed_at } , status : :ok
40- else
41- render json : { errors : pci . errors . full_messages } , status : :unprocessable_entity
35+ respond_to do |format |
36+ if pci . save
37+ Rails . logger . info ( "DBG PersonChecklistItemsController#create: saved pci id=#{ pci . id } completed_at=#{ pci . completed_at } " )
38+ # If checklist completed, trigger a hook (implement as ActiveSupport::Notifications for now)
39+ notify_if_checklist_complete ( person )
40+ format . json { render json : { id : pci . id , completed_at : pci . completed_at , flash : { type : 'notice' , message : t ( 'flash.checklist_item.updated' ) } } , status : :ok }
41+ format . html { redirect_back ( fallback_location : BetterTogether . base_path_with_locale , notice : t ( 'flash.checklist_item.updated' ) ) }
42+ format . turbo_stream do
43+ flash . now [ :notice ] = t ( 'flash.checklist_item.updated' )
44+ render turbo_stream : turbo_stream . replace ( 'flash_messages' , partial : 'layouts/better_together/flash_messages' , locals : { flash : } )
45+ end
46+ else
47+ format . json { render json : { errors : pci . errors . full_messages , flash : { type : 'alert' , message : t ( 'flash.checklist_item.update_failed' ) } } , status : :unprocessable_entity }
48+ format . html { redirect_back ( fallback_location : BetterTogether . base_path_with_locale , alert : t ( 'flash.checklist_item.update_failed' ) ) }
49+ format . turbo_stream do
50+ flash . now [ :alert ] = t ( 'flash.checklist_item.update_failed' )
51+ render turbo_stream : turbo_stream . replace ( 'flash_messages' , partial : 'layouts/better_together/flash_messages' , locals : { flash : } )
52+ end
53+ end
4254 end
4355 rescue StandardError => e
4456 Rails . logger . error ( "PersonChecklistItemsController#create unexpected error: #{ e . class } - #{ e . message } \n #{ e . backtrace . join ( "\n " ) } " )
45- render json : { errors : [ e . message ] } , status : :internal_server_error
57+ render json : { errors : [ e . message ] , flash : { type : 'alert' , message : e . message } } , status : :internal_server_error
4658 end
4759
4860 private
0 commit comments