Controller function - Length

Hi all, what you used to do when your controller function gets lengthy sometimes ? a way i found is just split the function. What is the real and Rails way to keep my code cleaner and maintainable. Thanks, Lekha.

Use a service object.

Google “Service Object Rails” for more.

Probably some of the logic should be removed from the controller and put into methods in the model(s). So for example look at the code and anywhere you have a few lines that are to do with extracting data from the model and manipulating it in some way then consider writing a model method that implements that function.

You can also extract bits of code into private methods of the controller, but concentrate first on moving stuff into the models.

Finally, if there is a lot of code interpreting data encoded in the url then maybe the routes could be improved.

Post one of the methods that you want to re-factor if you need some pointers.

Colin

As we say in rails skinny controllers, fat models. Ideally most of the business logic, all database interactions should go to model, becz model is meant to do it.

controller methods should only contain request and response code, so that it is easy to test.

Regards

Hi all, Thank you.