3

I made a mountable engine. In the engine I made a helper (located in /app/helpers/my_engine) which looks like this:

module MyEngine
  module ApplicationHelper
    def link_to_login(label = "Login", options = {})
        link_to label, some_path, options
    end
  end
end

In the engine.rb I added this code:

   initializer 'my_engine.action_controller' do |app|
      ActiveSupport.on_load :action_controller do
        helper MyEngine::ApplicationHelper
      end
    end

Everything works fine, but, when I load the engine in another app and use the helper function, I get this error:

Undefined method `some_path' for MyEngine:Module

When I use a path of the engine in the parents app, I call the path this way: my_engine.some_path, I do this for the alias of the namespace, in routes.rb:

mount MyEngine::Engine => "/my_engine", :as => "my_engine"

How should I call the path in the engine's helper? Because anyone can change the alias of the route, it is not a good idea to put my_engine.some_path inside the helper; what is the way to do this?

UPDATE: routes of the engine.

   MyEngine::Engine.routes.draw do

      #Devise for Users
      devise_for :users, {
        class_name: 'MyEngine::User',
        path_names: {sign_in: "login", sign_out: "logout"}, 
        :path => "u", 
        :controllers => { :registrations => "my_engine/users/registrations", :sessions => "my_engine/users/sessions" },
        module: :devise
      }

      match 'auth/:provider/callback', to: 'auth#create'
      match 'auth/failure', to: redirect('/')

      #Management of Users
      resources :users

      #Devise for Admins
      devise_for :admin_users, {
        :class_name => "MyEngine::AdminUser", 
        path_names: {sign_in: "login", sign_out: "logout"}, 
        :path => "d", 
        :controllers => { :registrations => "my_engine/admin_users/registrations", :sessions => "my_engine/admin_users/sessions" },
        module: :devise
      }

      #Management of Admins
      resources :admin_users

    end

UPDATE #2: Maybe, the right way is to use the proxy path. Check http://edgeapi.rubyonrails.org/classes/Rails/Engine.html and Named routes in mounted rails engine

Community
  • 1
  • 1
pablomarti
  • 2,087
  • 2
  • 22
  • 35

1 Answers1

3

Have you tried with engine.some_path this will make reference to the some_path inside engine. assuming some_path is inside the engine; or main_app.some_path if some_path is in your main app

joseramonc
  • 1,811
  • 2
  • 21
  • 40