The ruling delivered a major setback to Democrats nationwide.