Rails Controller Inheritance vs. Concerns and Mixins

假如想象 提交于 2019-12-24 05:17:09

问题


I have a lot of similar resources in my Rails application, and I have currently DRY'd up the code using controller inheritance. I see that there is a directory called concerns under the controller folder, where I could potentially write similar concerns (such as archiving, activate/deactivate etc.) I can probably write mixins too.

Is there a preferred approach to DRY up the controller code? Is there any downside in using inheritance, or are there any advantages using other techniques?


回答1:


Is there a preferred approach to DRY up the controller code?

In my experience, it depends on what you want the code to do. I've only used concerns for simple controller-independent methods, such as before_action or something

I would use inheritance if your controller's methods are able to rely on a super class or something. My experience so far has lead me to use inherited_resources - a DRY way to create controller inheritance



来源:https://stackoverflow.com/questions/22604996/rails-controller-inheritance-vs-concerns-and-mixins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!