BOISE, Idaho — 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 9th Circuit panel 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 [e]stablishment [c]lause 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.

“Criminally punishing homeless people for sleeping on the street when they have nowhere else to go is inhumane, and we applaud the court for holding that it is also unconstitutional,” Foscarinis said in a statement.

Boise spokesman Mike Journee said city attorneys were considering their next steps, which could include asking the full 9th Circuit to reconsider the ruling or appealing to the U.S. Supreme Court.

But city officials also believe the ruling validates the approach Boise police officers have taken since 2014 — not ticketing homeless people when shelters are full, he said.

“If there are, as a result of the ruling, adjustments that need to be made, we’ll consider those when the time comes,” Journee said.

Other cities have faced similar lawsuits with varying results.

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, Ore., 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, Wash.

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.”

Associated Press reporter Phuong Le contributed to this story from Seattle.