There are two potential issues with your approach, but the answer depends on the relay coil current. 1) the opto has a property called current transfer ratio, which means that the transistor side can only source / sink a certain percentage of the LED current. This means that, depending on the device chosen, you may not be able to directly drive the relay from the opto. 2) you need to consider the power limitations of the devices. If you drop from 24V to ~5V, you will need to be sure you don't exceed the power requiremetns of the resistor, which would easilly happen whem dropping 24V.
A better apporach would be to use the opto to drive a switching transistor that in turn controls the relay. This would avoid problem number 1, but you would still need to have a solution to provide the proper voltage. Perhaps you could use a small 3 pin Vreg or a zener diode. The relay will be somewhat tollerant of the input voltage, but it will still need to be about 5V.