Rails可安装的发动机路线无法识别

时间:2013-03-19 18:13:38

标签: ruby-on-rails rails-routing rails-engines

我做了一个可安装的发动机。在引擎中我做了一个帮助器(位于/ 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放在帮助器中并不是一个好主意;这样做的方法是什么?

更新:引擎的路由。

   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 :也许,正确的方法是使用代理路径。查看http://edgeapi.rubyonrails.org/classes/Rails/Engine.htmlNamed routes in mounted rails engine

1 个答案:

答案 0 :(得分:3)

你试过engine.some_path这会引用引擎内的some_path吗?假设some_path在引擎内;或main_app.some_path如果some_path在您的主应用程序中