Reading my comment it does sound harsh 😉
cycling tips does have a lot of good stuff. As above and a couple of others
https://cyclingtips.com/tag/cramping/
I do agree with antigees comments on moving about. Stretching out, changing position can help over long rides. If you are hungry or dehydrated it will make things worse (same way you would find it tougher riding really).
If you consistently get cramp in one muscle group you could do some research on muscle groups. You position can activate some muscles more that others or stress them.
But ultimately if you are pushing yourself hard and you cramp it is a fitness thing. Happens to the pros too when they go for heroic attacks. Jens Voigt made a whole marketing image around it!