0

I am writing an authentication engine that has an authenticate! method in its application controller along with a current user helper method etc.

I would like to be able to call this authenticate! method inside my main application and have it use the current_user that is set by the engines application controller.

After doing a little bit of googling, I ran across this post How to access Rails Engines methods from main application? but that wasn't helpful and neither were any of the suggested links within.

Right now inside my engine.rb file I have the following code:

require 'my_engine'

module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
    initializer "my_engine.load_helpers" do
      ActiveSupport.on_load(:action_controller) do
        include MyEngine::Helpers
     end
   end
 end

end

But this gives me the following error

uninitialized constant MyEngine::Helpers

What would be the best way to go about getting this done if it is possible?

Before starting this, the way I had things done was to set a session[:current_user_id] which I then passed to a current_user method in the main controller which also housed the authenticate! method that sent the user to the authentication engine. It would be nice if I can just call authenticate! in the main application and have the engines application controller handle that method and set the current user.

Community
  • 1
  • 1
Vell
  • 317
  • 1
  • 3
  • 15
  • I just happened to find something on http://edgeapi.rubyonrails.org/classes/Rails/Engine.html . Now I get undefined method `authenticate!'. This would only make it so that I could use the helpers but I think that may work for what I would like to do. – Vell Jul 01 '14 at 18:43
  • So it looks like since the helper is just a module I can use include MyEngine::ApplicationHelper and that allows me to at least use authenticate method the way that I want to but I can't seem to figure out how to set the current_user. – Vell Jul 01 '14 at 19:05
  • OK, so its available in my controller but not in my view. If I call current_user in a controller method it returns the expected result but inside of a view it returns undefined local variable. – Vell Jul 01 '14 at 19:11
  • I needed to put helper_method: current_user in my main applications controller. I was hoping I could put this in the engines application helper but I guess not. I have no idea if I did any of this correctly so definitely looking for critiques on the right way to do this. – Vell Jul 01 '14 at 19:16

0 Answers0