gabriela,
Rail passes are generally not cost effective, especially in Italy. Any routes using the premium / high speed trains require compulsory seat reservations, which are not included with Rail passes. You'd need to pay separately for the reservations, which are specific to train, date and departure time. If you're caught without a valid reservation for the train you're riding on, you'll face hefty fines which are collected on the spot! Also, I don't believe the other high speed rail network in Italy, Italo Treno, accepts Rail passes.
If you know specifically which trains you'll be using you can buy advance tickets online and save some money - https://www.trenitalia.com/en.html OR https://www.italotreno.it/en . You could also buy tickets for rail networks in both Italy or Spain at https://www.thetrainline.com/?gad_source=1&gclid=EAIaIQobChMI5syk6ZjkhQMVuR-tBh2mwgPSEAAYASAAEgLApPD_BwE (they may charge a small service fee, but I don't mind that as its nice to have "one stop shopping").
If you're travelling on Regionale trains in Italy, there's no advantage to buying in advance, as the price is the same. I normally just buy Regionale tickets as needed using the self serve kiosks or staffed ticket office in the stations. Another point to note is that paper tickets bought from the machines need to be validated prior to boarding the train using the machines in the station. Failure to do so can also result in hefty fines collected on the spot! Also note that Regionale tickets purchased online have to be "activated" online prior to the trip as that "validates" them.
I haven't researched rail tickets in Spain for awhile, so can't provide any comments on that. Hopefully one of the others can provide more information.