There are many causes of cramps, each with it's own corresponding cure.
What is a cramp?
Cramps are strong, involuntary muscle contractions. They can occur at any time but are most common during or shortly after hard exercise. They can occur in any muscle, though in cyclists they are most common in the quads, hamstrings and calves. They can be so strong that they cause you to launch out of a chair or actually pull a muscle.
Cramps have many causes, though fundamentally they are similar. When you move, your brain sends signals to your muscles requesting a contraction. The brain receives feedback on the strength of the contraction that has occurred, from which it can make adjustments to create a controlled movement. If the feedback says that the contraction is harder than expected, the brain can send instructions to contract less. If the feedback says the contraction is weak, the brain can send a signal to contract more. As a muscle fatigues, the brain sends more signals to tell the muscle to contract to get the same strength of contraction. When the muscle becomes too fatigued to do what is asked of it, the brain sends a continuous contraction signal, initiating a cramp.
Causes of cramps and how to correct them
Anything that fatigues a muscle can bring on a cramp, and anything that keeps a muscle fresh helps prevents cramps.
Inadequate training: You may cramp late in a long or hard ride simply because you have not trained adequately for the distance or the intensity. Make gradual increases to volume and intensity.
Pushing big gears: One clue that you may be doing this is if you find yourself standing each time you need to accelerate. Another clue is measuring your cadence below 85 rpm for much of a hard ride. The cure? Switch to a lower gear. Spin to save your legs.
Dehydration: Muscles don’t contract well if they don’t contain their normal amount of water. Stay hydrated.
Fuel: Muscles can’t contract if they don't have a good supply of glucose. Keep eating carbohydrate rich foods on longer rides. Eat something at the start of the ride, after about 30-40 minutes and every 15-20 minutes thereafter. Aim for about 250 calories per hour if you are under 150 pounds and 300 if you are over 150 pounds.
Electrolyte balance: Muscles will cramp if they don't contain their normal amounts of sodium, potassium, calcium and magnesium. Those amounts change during exercise. Salt your food and eat plenty of bananas. If you don't eat a lot of dairy, take a calcium supplement or eat plenty of brassica veggies (collards, kale, cabbage, broccoli, Brussels sprouts and cauliflower).
If your cramps affect more than one muscle group, than electrolyte imbalance or dehydration is likely the cause. It is important to recognize that the primary electrolytes lost in sweat are those that are found in the greatest amounts in extra cellular fluid--sodium and chloride. Unfortunately, people who have high concentrations of sodium in their sweat, i.e., "salty sweaters", and/or who have high sweat volumes, lose sodium at a higher rate than it can be replaced. These individuals may lose up to 1500 mg of sodium per hour, while the recommended rate of consumption is 500-700 mg per litre of water per hour, which is based on the rate at which sodium is absorbed from the gut. Not all sports drinks are equal in how much sodium they provide, so read the label!
Calcium, magnesium, and potassium are also lost in sweat, although in much smaller amounts than sodium. However, you want to be sure to consume the recommended intakes of these electrolytes. Calcium is found in dairy products and fortified juices and other foods. Potassium is highest in fresh fruits and vegetables, meat and dairy products. It it somewhat more challenging to get enough magnesium in the diet as the best sources are green leafy vegetables, some beans and legumes, and unrefined grains. You might consider taking a magnesium supplement that provides the RDA or less. However, don't take it at the same time as a calcium supplement, as they will interfere with each other's absorption.
Raw Spinach: Some leafy green vegetables eaten raw, particularly spinach, will leach calcium from your system. Avoid large amounts of raw spinach. Cooked is okay.
Creatine Monohydrate supplementation: In some people creatine supplementation (especially loading) may cause cramps, especially if the athlete is dehydrated. If in doubt, avoid this supplement.
Tight muscles: Regular stretching of muscles that tend to cramp sometimes reduces cramping.
Impaired circulation: Muscles that are not receiving adequate blood supply are deprived of oxygen and fuel. They will not recover from one contraction to the next and so will fatigue quickly. Correct pressure points on the saddle, in your shoes, in your shorts and anywhere else they might interfere with circulation.
Heat or cold: On hot or cold days some people will cramp even if they do everything else right. On hot days, do what you can to keep cool. As well as staying hydrated, dribble water on your jersey and shorts and through your helmet every once in a while. Choose shadier and flatter routes on hot days, unless you are racing and don't have a choice. On cold days, dress warmly.
Bike Fit: A poor bike set up may cause some muscles to work harder than necessary, bringing on a cramp. If in doubt check your fit.
Rhabdomyolysis: If cramps are followed by red or brown urine, you are experiencing a breakdown of muscle tissue and release of muscle contents into the blood. This is a medical problem needing immediate professional attention to prevent kidney damage. Treatment for acute rhabdomyolysis is high volume IV rehydration.