简体   繁体   中英

Rails Controller Inheritance vs. Concerns and Mixins

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM