Praise be to Allah.
The correct scholarly view is that masturbation is haram .
If a person masturbates at night in Ramadan, that does not invalidate his fast, either on the day before or the day after.
But the Muslim must strive to control himself and avoid doing this haram action and others, especially during this blessed month.
The Muslim has to make the most of this blessed month , for fasting is the best treatment for this haram thing. Hence the Prophet (peace and blessings of Allah be upon him) commanded young men who were unable to get married to fast. The Prophet (peace and blessings of Allah be upon him) said: “O young men, whoever among you can afford it, let him get married , and whoever cannot afford it, then he should fast, for that will be a shield for him” – i.e., fasting will curb his desire.
Whoever has been tested with this must repent to Allah and regret what he has done, and resolve not to do it again.
And Allah knows best.