小编And*_*kov的帖子

Rails:如何访问Observer中的当前控制器?

我需要访问当前控制器或从观察者方法中刷新通知.

class SomeObserver < ActiveRecord::Observer
    observe :some_model
    cattr_accessor :current_controller

    def after_create(record)
        ...
        current_controller.flash[:notice] = "Some message!"
    end
end

class ApplicationController < ActionController::Base
    before_filter do
        SomeObserver.current_controller = self
    end
    ...
end
Run Code Online (Sandbox Code Playgroud)

ruby controller ruby-on-rails observer-pattern

5
推荐指数
1
解决办法
1539
查看次数