There are many reasons ahorse wants to drift off the rails, with reason being the cause, and the horse's ability to do so, lack of quide and softness to aids
Horses that have been shown a lot, on the rail, soon learn that when they go to center, the work for that class at least is done, esp after they have reversed direction.
Plain boredom or that horse constantly being schooled on the rial, so that the rail becomes a bad place to be
Thus, never school on the rail. Do your schooling on the middle of the arena, and then take to horse to the rail, where you let him be, trust him to stay correct, making the rail a good place to be.
Do you also ride this horse out, or do you ride endlessly on the rail in an arena?
I don't school ahorse to follow a rail, quite the opposite! I get control of the horse;s body, so he learns to stay evenly between my reins and legs.
Horses follow their shoulders, until we teach them to follow their nose, with their entire body in correct aleignment.
I would forget about riding the rail for now, and really get some body control, and lightness. Then, when he does drift in from the rail, take him to that center of that arena and then really school what the problem is. Get him really moving off of that inside leg and rein that he moved against. Yes, this is a time to apply that spur. Then take him back to the rial, and give him a chance to do the right and easy thing-respect that inside leg and rein