Background – I have a model, say Door, that has a state of open or closed. I encapsulate the behaviour of opening the door in a method #open on each instance (And I also have a #close equivalent).
But what’s the best way to expose this in a RESTful way? What should my route be?
It is an UPDATE to Door instance, but what should I UPDATE with?
Cheers,
Gordon
There is no one best way that everybody would agree on.
Some people would add two custom actions
openandcloseto their controllers, which would then call the respective methods on theDoormodel.Other people would use the
updatecontroller method to change whatever attribute the door uses to store its open/closed state. e.g. a boolean fieldclosed. They might also argue that you should not have dedicatedopenandclosemethods besides theclosedattribute, and that you implement with validations and callbacks whateveropen/closeused to encapsulate.My recommendation is not to worry too much about RESTfulness and always choose the most practical way.