First of all, they didn't do anything as they manage to get only in some colleges and get to some of the ballots. The only thing they did achieve is that the whole world is looking at that and see police brutality.
How could they deal with it? Since it was known that referendum will take place, maybe they could get there first and closed it in the start before there were already hundreds of people. That way they're the one that has to be moved out of the place.
But since we live in the democratic world I'm pretty certain that the best way would be to allow that referendum and beat it with legal arguments if they can. Not police sticks and boots.
First, neither Police nor Guardia Civil (or both together) had the manpower to close every school before the vote started, they can only be in X points at the same time, they tried to avoid this days ago when they started the requisition of ballots and pro-referendum items, but it didn't work. Extending on that, the ballots weren't there before the referendum started, they reached the schools when people were already there to make sure they got in as you can see here
Second, the legal arguments have been there for days, weeks, months and years and the Catalan government ignored the law and went ahead. This was the 3rd time they tried to get a referendum.
The first was in 2014, Artur Mas tried to do it and it was legally repelled by the constitutional Court. So Mas reaction was changing it from a referendum to a query
Second was last year 2016, after the failed referendum they used the election as a "de facto" referendum.
And this was the third time, that was ruled unconstitutional again but it didn't matter, even the fact that they wanted to use public money for the infraestructure to held this referendum was unconstitutional so they had to fund it "privately".
So, when you have a bunch of people that showed 3 times in 4 years that they don't give a damn about your constitution, when there's sentences against some of those people and all of that, and they keep heading towards the same way, what are you supposed to do? The legal way was depleted because the Catalan government has shown that they have 0 regards towards that.
That leads to a second question, "why isn't there a movement to change the constitution and allow that". Well because this already happened 13 years ago with the Basque country and that way finished with both Basques and Spanish getting to an agreement, second the PP and PSOE have been adamant about changing the constitution (bar the exception when they were forced by Europe) and even less to open a way to break what we now know as Spain, and lastly because the Catalan government have the momentum, have been building towards this for years and Rajoy/PP are the best chance they're gonna get to face a central government that's doomed to fail keeping Catalan interests relevant while being inept enough to fuel even more the independent feeling.
And to round it all, democratic world doesn't mean "We'll do whatever the hell we want, we just need to vote it and make it win", there was a constitution voted 40 years ago that won by an appaling 92%, IF the Catalan government goal was to change the constitution to be able to someday in the future held a legit referendum it would be one thing that, with time, could end up happening, but for the last 5 years they have shown more than enough times that for them the referencum is only a tool to their real goal, the independence of Catalunya, and they're going to try and achieve that goal either by a legal or non-legal way. As the Spanish government, why would you open the door to a referendum for people that, even with that door closed have held 3 different ones in just 4 years? It's clear that once you open that door you're going to eat referendums yearly until the results are the ones desired.