3

我做了一个可安装的引擎。在引擎中,我创建了一个助手(位于 /app/helpers/my_engine),如下所示:

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

在 engine.rb 我添加了这段代码:

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

一切正常,但是,当我在另一个应用程序中加载引擎并使用辅助函数时,我收到此错误:

Undefined method `some_path' for MyEngine:Module

当我在父应用程序中使用引擎的路径时,我以这种方式调用路径:my_engine.some_path,我在 routes.rb 中为命名空间的别名执行此操作:

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

我应该如何调用引擎助手中的路径?my_engine.some_path因为任何人都可以更改路由的别名,所以放在helper 里面不是一个好主意;这样做的方法是什么?

更新:引擎的路线。

   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

更新#2:也许,正确的方法是使用代理路径。检查http://edgeapi.rubyonrails.org/classes/Rails/Engine.html已安装的 Rails 引擎中的命名路线

4

1 回答 1

3

您是否尝试过engine.some_path这样做会引用引擎内部的 some_path。假设 some_path 在引擎内部;或者main_app.some_path如果 some_path 在您的主应用程序中

于 2013-12-16T04:52:04.317 回答