I don't think you can keep them out of the gym entirely. You should expect that parents will want to see what their kids are doing through the class. I wouldn't take my kids somewhere I couldn't at least watch from a distance.
That said, there should be firm limits. I've trained in schools where the parents are permitted to coach their kids or even get on the mat themselves to give advice. This is bullshit, creates confusion about who is in charge, and leaves the kids listening to incompetent technical instruction.
I've always advocated for a hard "zero communication over the mat boundary" policy. If you're training, you're on the mat and the coach is the one running training. No one else on the mat gives advice unless asked to do so by the coach. If you're off the mat, you don't talk to people training even if you're the head black belt of the academy. Other students, and especially non-training parents, don't talk to people on the mat about anything. Definitely no trans-mat socializing. Obvious exceptions to this rule are injury and/or imminent blood/vomit situations, and open mat sessions.
(Edit: Yeah, exactly what Cotalis describes.)