Rails makes it easy to adapt Restful architecture. All you have to do is following.
I started putting all pictures related activities in
In the beginning it was simple.
Slowly the application evolved. The application started handling two different types of pictures. There would be pictures for events and then there would be pictures of users using the system.
One can add comments to the event pictures but one can’t add comment to user pictures. Slowly the requirement for event pictures grew vastly different from user pictures.
Sounds familiar. Right. Initially controller takes on a few responsibilities but slowly the controller starts taking a lot more responsibilities and then controller becomes huge.
The pictures controller was really huge and was fast becoming a mess and specially writing test was getting very difficult.
Time had come to create two different controllers: one for event pictures and one for user pictures.
But wait. Lots of people would say that if we want to be restful then there has to be one to one mapping between the model and the controller. Not true.
Model != resource
Being restful does not mean that there has be a one to one mapping between the model and the controller.
I am going to create a new controller called
which will take on all the functionality related to users dealing with picture.
And this is going to be restful.
Above I have defined a resource called
To keep it simple this controller would do only three things.
- display all the pictures of the user ( index )
- allow user to upload pictures ( create )
- allow user to delete a picture ( destroy )
That’s the general idea. In my application I have only three actions.
However in the interest of general discussion I am going to show all the seven methods here. Also for simplicity create in this case means adding a record (I am not showing multipart upload).
Here is the code for controller.
Another use case
Let’s talk about another example.
Let’s say that we have a model called
besides the regular functionality of creating, deleting, updating and listing projects,
one needs two more actions called enable and disable project.
Well the projects controller can easily handle two more actions called “enable” and “disable”.
However it is a good idea to create another controller called
This controller should have only two actions -
destroy in this case would mean disabling the project
create would mean enabling the project.
I know it looks counter intuitive. Actions ‘enable’ and ‘disable’ seem simpler than “create” and “destroy”. I agree in the beginning adding more actions to pictures controller looks easy. However if we go down that path then it is a slippery slope and we do not know when to stop.
Compare that with the RESTful design of having only seven action :
This limits what a controller can do and that’s a good thing.
This ensures that a controller does not take up too many responsibilities.
Creating another controller allows all the business logic which is not related to one of those seven actions to be somewhere else.
One last example
Now that we have the ability to “enable” and “disable” pictures how about showing “only active”, “only inactive” and “all” pictures.
In order to accomplish it once again we can add more actions to the pictures controller.
However it is much better to have two new controllers.
Some of you must be thinking what’s the point of having a controller for the sake of having only one action. Well the point is having code that can be changed easily and with confidence.
In this blog I tried to show that it is not necessary to have one to one mapping between model and controllers to be restful. It is always a good idea to create a separate controller when the existing controller is burdened with too much work.