Cramping is typically due to lack of fitness/endurance, or rather, pushing past your current fitness/endurance. Rarely is it actually dehydration (assuming most people eat and drink at least some on longer rides). Proper pacing for the distance, and continuing to train for more endurance, are the best way to prevent cramps.