2012-05-23 59 views
9

我正在構建一個名爲Engrave的Rails引擎。在rails引擎視圖中缺少設計路徑助手

我必須裝在發動機上,像這樣:

# Routes.rb of the host app 
mount Engrave::Engine => "/engrave", :as => "engrave_engine" 

在這臺發動機我有一個名爲「PostsController」控制器。當我瀏覽到該控制器查看後,像這樣:/engrave/posts/1我得到這個錯誤:

undefined local variable or method `new_user_session_path' 

的PostsController發動機從發動機控制器,它從應用控制器繼承,像這樣繼承:

module Engrave 
    class PostsController < ApplicationController 
    ... 
end 

class Engrave::ApplicationController < ApplicationController 
end 

的new_user_session_path正在由色器件所定義,其中我有安裝像:

devise_for :users 

到new_user_session_path呼叫是layouts/application.html.erb模板文件中的主機應用程序

我不明白爲什麼此路線幫助程序在此上下文中不可用。我究竟做錯了什麼?

+0

我將StrangeDays答案標記爲正確,因爲它在技術上解決了問題,但如果任何人有了這種方式使用引擎而不需要修改所有佈局模板的想法,那就太棒了。我四處遊玩,並設法通過創建一個完整的引擎而不是一個孤立的/可安裝的引擎來實現我想要的。我想我仍然錯過了一些東西,我想要吃我的蛋糕,然後在這一塊上吃。 – Jeff

回答

9

使用

main_app.new_user_session_path

應該工作

6

我已經成功做在主應用程序的application_helper.rb如下:

module ApplicationHelper 
    # Can search for named routes directly in the main app, omitting 
    # the "main_app." prefix 
    def method_missing method, *args, &block 
    if main_app_url_helper?(method) 
     main_app.send(method, *args) 
    else 
     super 
    end 
    end 

    def respond_to?(method) 
    main_app_url_helper?(method) or super 
    end 

private 

    def main_app_url_helper?(method) 
    (method.to_s.end_with?('_path') or method.to_s.end_with?('_url')) and 
     main_app.respond_to?(method) 
    end 
end 

我用這在可安裝的引擎中,所以你不必犧牲這些功能。

0

工作過的this response,我包括通過陳述helper "manager/application"控制器內部的application_helpers.rb找到的所有助手(如果「經理」是你安裝發動機的當前的命名空間。只是,如果你是從調用這個使用「應用」標準應用程序)。