Cities can't prosecute people for sleeping on the streets if they have nowhere else to go because it amounts to cruel and unusual punishment, which is unconstitutional, a federal appeals court said Tuesday.

The 9th U.S. Circuit Court of Appeals sided with six homeless people from Boise, Idaho, who sued the city in 2009 over a local ordinance that banned sleeping in public spaces. The ruling could affect several other cities across the U.S. West that have similar laws.

It comes as many places across the West Coast are struggling with homelessness brought on by rising housing costs and income inequality.

When the Boise lawsuit was filed, attorneys for the homeless residents said as many as 4,500 people didn't have a place to sleep in Idaho's capital city and homeless shelters only had about 700 available beds or mats. The case bounced back and forth in the courts for years, and Boise modified its rules in 2014 to say homeless people couldn't be prosecuted for sleeping outside when shelters were full.

But that didn't solve the problem, the attorneys said, because Boise's shelters limit the number of days that homeless residents can stay. Two of the city's three shelters also require some form of religious participation for some programs, making those shelters unsuitable for people with different beliefs, the homeless residents said.

The three-judge panel for the 9th Circuit found that the shelter rules meant homeless people would still be at risk of prosecution even on days when beds were open. The judges also said the religious programming woven into some shelter programs was a problem.

"A city cannot, via the threat of prosecution, coerce an individual to attend religion-based treatment programs consistently with the Establishment Clause of the First Amendment," Judge Marsha Berzon wrote.

The biggest issue was that the city's rule violated the U.S. Constitution's Eighth Amendment against cruel and unusual punishment, the court found. The amendment limits what the government can criminalize, it said.

"As a result, just as the state may not criminalize the state of being 'homeless in public places,' the state may not 'criminalize conduct that is an unavoidable consequence of being homeless — namely sitting, lying, or sleeping on the streets,'" Berzon wrote.

The ruling shows it's time for Boise officials to start proposing "real solutions," said Maria Foscarinis, executive director of the National Law Center on Homelessness & Poverty, whose attorneys were among those representing the homeless residents.

In 2007, the 9th Circuit ruled in favor of homeless residents of Los Angeles, finding that as long as there are more homeless residents than there are shelter beds, a law outlawing sleeping outside was unconstitutional. Both sides later reached an agreement and the entire case was eventually thrown out.

In 2009, a federal judge said a Portland, Oregon, policy designed to prevent people from sitting or lying on public sidewalks was unconstitutional. Portland officials now must also give campers at least 24 hours' notice before cleaning up or moving unsanctioned camps.

A state judge rejected a similar anti-camping law in Everett, Washington.

Sara Rankin, a professor at the Seattle University School of Law and director of its Homeless Rights Advocacy Project, said the ruling will serve as a wake-up call to local governments, forcing them to invest in adequate supportive housing for the chronically homeless.

"I think it's finally common sense," Rankin said of the ruling. "There are certain life-sustaining activities that people can't survive without doing. It's a really important recognition that people have to be able to legally exist and survive somewhere."