简体   繁体   English

Ruby on Rails:您是否建议使用Observers?

[英]Ruby on Rails: Do you recommend using Observers?

I notice it's used primarily for sending emails. 我注意到它主要用于发送电子邮件。 Let's say I want to send an email after every comment is created. 假设我要在创建每个评论后发送电子邮件。

Is using Observers really necessary when you could just place the Mailer.deliver_email(user) in your comments_controller.rb's create action instead? 当您仅将Mailer.deliver_email(user)放在comment_controller.rb的create动作中时,是否真的需要使用Observers?

For proper programming practices, yes. 对于正确的编程实践,可以。 The observers decouple the code and make sure it stays maintainable. 观察者将代码解耦,并确保代码保持可维护性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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