A blow to the city Department of Education, the ruling underscores the grueling process to fire teachers accused of misconduct or incompetence. Even after lengthy administrative trials, judges can reverse decisions either way, and prolong a costly battle.

“After today, I’m thinking the beach sounds like a wonderful idea for my 5th graders. I HATE THEIR GUTS! They are all the devils spawn!” Rubino wrote on Facebook on June 23, 2010.

She made the remarks one day after a 12-year-old Harlem girl, Nicole Suriel, drowned on a school trip to a Long Island beach.

When a Facebook friend asked, “oh you would let little Kwame float away!’’ Rubino posted in reply, “Yes, I wld not throw a life jacket in for a million!!”

While “offensive” and “repulsive,” the comments were posted outside the school building and after hours, Jaffe ruled.

The judge found no evidence that Rubino, frustrated by her students’ rowdy behavior, meant them actual harm or that the comments “affected her ability to teach.”

Jaffe even gave Rubino a pass for allegedly obstructing an investigation. A friend of Rubino admitted she lied when she claimed authorship of the ugly comments, but Rubino denied asking her to do so. Rubino’s “clumsy attempt at a cover-up reflects panic, not planning,” the judge wrote.

In firing Rubino last June, hearing officer Randi Lowitt said Facebook and other social media “are becoming embedded in society.” Lowitt chided, “People post without regard to the fact that what they post has a shelf life of forever.”

Jaffe differed. “Even though [Rubino] should have known that her postings could become public,” the judge wrote, it was “reasonable” to expect that only her Facebook adult friends would see them.

Citing Rubino’s “unblemished” 15-year teaching history, Jaffe voided the termination and sent her back to the department for a “lesser penalty.”

Rubino, who made $78,885 a year, won’t go back on the payroll so fast because the department may appeal.

Rubino’s lawyer, Bryan Glass, said, “A simple warning to Ms. Rubino about her mistake would have sufficed, and it would not have been repeated. Perhaps in this time of scarce resources, the substantial time and money on this case could be better allocated by the DOE supporting its teachers in the classroom rather than demonizing and punishing its staff.”