Search code examples
ruby-on-railsrubyruby-on-rails-2

Rails 2.3.x: Is it possible to call a controller's action from Initializer?


I am using rails 2.3.8 . I need to call a controllers action from a initializer. I found this snippet in rails 3 usin Proc

    OmniAuth.config.on_failure = Proc.new do |env|
    UserSessionsController.action(:omniauth_failure).call(env)
    end

If i use this in Rails 2.3.8, it returns this error

   undefined method `action' for UserSessionsController:Class. 

It seems Rails 2.3.8 does not have "action" method. Is there any other way to call an action from initializer in Rails 2.3.x?


Solution

  • I'm not sure why you would want to do this, but controllers are just classes. So something like

    TestController.new.index
    

    will call the index method.

    Keep in mind that the controller will be initialized w/o all the info that normally comes in w/ a request, so its very possible that things will break.