learn it on boxes first and learn to lock in to the box as soon as you can..then you can try it on rails.
main thing is to get away from the habit of 270 on and then flat spinning on the box..ideally you want to two sev on and stop your rotation right away/try to pretzel.. once you can two sev and lock in, take it to an easy non urban flat bar or something close and try it on that.. if you got good at locking in, then you should have no trouble with the rail/not nutting yourself
how to stop rotation? at first, after you 2sev on to the box and land, push your lead ski forward and tilt it back so that the bottom of the ski is visible and pointing to the end of the box... once you get better, you can start doing a pedal, where the front part of your ski literally breaks or pushes against the side of the box causing you to stop the rotation and eventually to backside switchup (revert) or pretzel 2sev out.
last piece of advice..once you learn two sev on, do it onto everything you can as often as you can..learn your unnatural side too (pref. after you learn unnaty 3's pretty well).. this is a trick that takes repetition to really master