I really think that dogs who can heel very nicely elsewhere and who get sloppy in the ring are just reacting to stress. I've been trialing forever (well, feels like forever!) and have had my share of sloppy performances and I really believe that the dogs don't deliberately do what we don't want them to do. They get stressed, they feel our stress, and they mentally distance themselves from us in order to try to alleviate their feelings of anxiety at that point.
One of the things that I found really difficult to get myself to do was to always be positive in the competition ring. That means if my dog does something other than what I want, I need to laugh and clap my hands and say "oooh, silly girl, get back here!" in a very happy, positive tone. Yes, we may lose more points than if I gave a single "heel" command but what I've done at that point is help diminish my dog's stress and that's going to help us the NEXT time we go into the ring. If I act stern or upset, the dog is going to associate that with being in competition and even though it may (at that moment) bring the dog back into heel position it will probably create more problems later on.
It's very hard not to show some disappointment in our dogs when they don't perform at the level that we know they can handle, especially when they've just blown a $25 entry fee (easy to do in regular obedience, rally is a bit more forgiving). But if the plan is to continue to trial this dog, you really have to just shrug it off and be happy and upbeat even when you're thinking "but I KNOW she KNOWS it!".
Leaving the ring to see your DH was probably stress related, too. After all, your dog had the option to stay in the ring with you (the person who was probably tense and commanding) or go out of the stressful area to see her other buddy (who was probably giving much more relaxed signals). Being by the ring gate is probably not the best place for DH, but I really wouldn't be too upset with the dog or with DH. It happened, it's over, it was just a rally trial. To proof against that, you can work her with him nearby and do a lot of happy talk to her (along with toy or treat rewards) every time you go past him and she continues to respond to you. At first if she glances at him and back to you, she gets rewarded. And if she tries to go to him, you just stop and wait (ideally she's on leash at this point). No correction, no commands, you just wait. And he just stands there and waits silently too, with no reaction to her even if she gets all the way to him. When she finally looks back at you or returns, you praise and start forward and after a few steps stop and reward her happily.
When you do this over and over, you will soon have her figure out that going to him gets her nothing, going back to you gets her everything. She'll have much less reason for wanting to go to him when you're working with her.
I went to a Denise Fenzi seminar this weekend and she had us doing that with our dogs. With Khana, we were to the point where the other person was actually holding out very good tempting treats as I heeled by (within reach of those treats) and Khana was heeling with me without going to the person. She did go to her at first, but when she figured out that there was no reward when she did that she chose to stay with me instead. In fact, the person trying to entice her with the treats became a cue for her to look at ME instead. It was really fun and absolutely positive.
Another thing that really helps with the ring distractions is to go to fun matches and to plan on using the match as a training exercise and not as a true run-through. Put your treats/toy on a chair right outside the ring where you can reach for it over the ring gate, and then plan on just doing a short amount of work (like maybe just a heel pattern, or just 5 or 6 rally signs - and then when she's working nicely, break her out of it, run to the ring gate and reach over for your toy/treats, and spend a couple of minutes praising and playing and rewarding her. This helps the dog realize that the ring is a GOOD place and not a stressful place.
Melanie and the gang in Alaska
(where there are few fun matches, unfortunately)