obj = resource_instance obj.assign_attributes(items_completion_params) if params[:update] ? obj.save : obj.complete_items respond_to do |format| format.html { redirect_to obj, notice: "#{params[:commit]} successfully completed." } format.json { render json: {status: :ok} } end else if params[:update] respond_to do |format| format.html { redirect_to obj, alert: obj.errors.full_messages.join(', ') } format.json { render json: {status: :bad_request, msg: 'Failed to update remark'} } end else flash.now[:alert] = 'Failed to complete' obj.items.select(&:_complete).each { |i| i.status = Dealing::STATUS_DEALING } render :edit end end end Revisit, Refactor, Rewrite def update result = Dealing::Save.call(params) if result.success? # ... elsif result['complete.failure?'] flash.now[:alert] = 'Failed to complete' render :edit else # ... end end class Dealing::Save < Trailblazer::Operation step Model( Song, :find ) step Nested { :build! } step Contract::Build( constant: Dealing::Update ) step Contract::Validate() step Contract::Persist() #=> save failure :error! def build!(options, params:, **) if params[:complete].present? Dealing::Complete elsif params[:update].present? self.class else fail 'Invalid submit value' end end end class Dealing::Complete < Trailblazer::Operation step Contract::Build( constant: Dealing::Complete ) step Contract::Validate() step Contract::Persist() #=> complete_items failure :error! failure :revert_status! def revert_status!(options, **) #... end end Save Complete