It is very difficult to get someone committed against their will, unless they have committed violent acts. After all, I'm still at large. In states with big public sectors, aid for the mentally ill -- assuming they accept assistance -- can be very spotty. There are agencies with phone numbers, but try calling and getting some service. (I have.) In Arizona, forget it.
The icing on the cake is that in some places almost everyone has access to firearms. Efforts to deal with guns or magazines or bullets are doomed, like Prohibition or 'The War on Drugs.' There are just too many around, and too many people who want them. Better coverage of mental illness would be worthwhile, but it wouldn't stop homicidal people from reflecting back the hostilities of extremists with prominent platforms. We know who the extremists are.
P.S. I would concede that some kind of gun control would make it more difficult for incompetent, crazy people like Nutboy to get guns. As crime control, however, forget it.
P.P.S. An attack of this intensity on a Congress person is an exceedingly rare event, so in that sense it's futile to diddle with 'how could this have been prevented.'