2011-11-06 19 views
8

私はレールエンジンに取り組んでおり、ヘルパーに問題があります。Helpers in Railsエンジン

明らかにこれは既知の「問題」ですが、そこには多くのソリューションはありません。問題は、私がグローバルにアクセスしたいAuthenticationHelperを持っていることですが、それは動作しません。

私はread that you could add a few lines to your init.rbですが、効果がないようです。

アプリケーションをエンジンで使用できるようにするにはどうすればよいでしょうか?

EDIT:代わりにコード(リンクから)をengine.rbに入れてください。

+1

あなたがの例を提供することができますあなたはengine.rbに入れたコードですか? – westonplatter

答えて

10

engine.rbにこのコードを入れてください:

config.to_prepare do 
    ApplicationController.helper(MyEngineHelper) 
end 
+0

クラスエンジン内 linjunhalida

+1

後継用:これは不要です - すべてのヘルパーを自動的にロードします – danielpcox

+3

レールを実行しています3.2.12 ...これをApplicationHelper内のメソッドにアクセスするには追加してください – Patm

2

私はこれを含める試みたエンジンの見解からメインアプリヘルパー(ApplicationHelper)にアクセスするには:

アプリ/ヘルパー/ your_engine/application_helper.rb

module YourEngine 
    module ApplicationHelper 
    include ActionView::Helpers::ApplicationHelper 
    end 
end 

それは動作しますが、私はdevのサーバーを再起動したときに一度、それは私uninitialized constant ActionView::Helpers::ApplicationHelperを投げるが、私はできませんこの例外を再現する。

EDIT

includeこれを削除し、この1作っ:

のlib/my_engine/engine.rb(それはエンジン内部です)

module MyEngine 
    class Engine < ::Rails::Engine 
    isolate_namespace MyEngine 
    config.to_prepare do 
     ApplicationController.helper(ActionView::Helpers::ApplicationHelper) 
    end 
    end 
end 
+1

Rails 4.2の.helper行を次のように変更しなければなりませんでした:ApplicationController.helper(:: ApplicationHelper)...それはあなたの最初の解決策でも働いているかもしれません。 – Allen