In my opinion, the simplest way to avoid pigmen spawning is to place top-slabs above the rails. You can still go through and quit the minecart.
For slopes, I just discovered that the opened fence gates above the rails do not stop the minecarts ! For slopes, I just discovered that the opened fence gates above the rails do not stop the minecarts. Edit : after some tests, pigmen can spawn in open fence gates :-(