Rails Observer Alternatives for 4.0

前端 未结 12 814
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 05:29

With Observers officially removed from Rails 4.0, I\'m curious what other developers are using in their place. (Other than using the extracted gem.) While Observers were cer

12条回答
  •  半阙折子戏
    2020-12-04 05:46

    They are in a plugin now.

    Can I also recommend an alternative which will give you controllers like:

    class PostsController < ApplicationController
      def create
        @post = Post.new(params[:post])
    
        @post.subscribe(PusherListener.new)
        @post.subscribe(ActivityListener.new)
        @post.subscribe(StatisticsListener.new)
    
        @post.on(:create_post_successful) { |post| redirect_to post }
        @post.on(:create_post_failed)     { |post| render :action => :new }
    
        @post.create
      end
    end
    

提交回复
热议问题