Rails makes it easy to adapt Restful architecture. All you have to do is
I started putting all pictures related activities in pictures_controller.rb . 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.
You can add comments to the event pictures but you can’t add comment to user pictures. And 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 in code size.
The pictures controller was really really huge and was fast becoming a mess. Especially 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 user_pictures_controller.rb that 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
user_pictures. 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 project and 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 project_status_controller . And this controller should have only two actions - create and destroy. destroy in this case would mean disabling the project and create would mean enabling the controller. I know initially it looks counter intuitive. Why would you know have actions called ‘enable’ and ‘disable’ which are much more easy to relate to compared to having another controller with methods ‘create’ and ‘destroy’. All I can say is that REST is a big topic. Do a search on REST and try reading a few books. Also the more you work developing RESTful controller the more you will understand about it.
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.