简体   繁体   English

如何在Rails中使用观察者gem

[英]How to use observer gem in rails

It is the first time to interact with observer gem, and after reading the wiki i just cant get it how would it send a message after creating some record in the database here is the code i used from the git hub wiki with changing the names to an existing model name in my project: 这是第一次与观察者gem进行交互,在阅读Wiki之后,我无法理解,它在数据库中创建了一些记录后如何发送消息,这是我从git hub Wiki中使用的代码,将名称更改为我的项目中现有的模型名称:

  class CommentObserver < ActiveRecord::Observer
      def after_save(comment)
        Notifications.comment("admin@do.com", "New comment was posted", comment).deliver
      end
    end

i dont know what this function do can anyone please explain it to me as it is not clear in the wiki,Thanks. 我不知道此功能有什么用,任何人都可以向我解释一下,因为在Wiki中尚不清楚,谢谢。

The after_save callback method is overridden to send a email notification. 重写after_save回调方法以发送电子邮件通知。 callbacks are methods which are called at specific points(moment) during the creation of an object. 回调是在对象创建期间的特定点(时刻)调用的方法。 so here the after_save is called when an Object of type Comment is saved. 所以在这里,保存Comment类型的对象时将调用after_save you can read more about callbacks here . 您可以在此处阅读有关回调的更多信息。

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

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