Helpers in Rails engine

懵懂的女人 提交于 2019-12-21 10:32:42

问题


I am working on a rails engine and I have a problem with the helpers.

Apparently this is a known "problem" but there's not a lot of solutions out there. The problem is that I have an AuthenticationHelper which I want to access globally - but it's not working.

I've read that you could add a few lines to your init.rb but it does not seem to have any effect.

Any idea what the best way to make an application available in an engine?

EDIT: Fixed it- Just put the code (from the link) in the engine.rb instead.


回答1:


Put this code in engine.rb:

config.to_prepare do
  ApplicationController.helper(MyEngineHelper)
end



回答2:


To access main app helpers (ApplicationHelper) from engine's views I tried include this:

app/helpers/your_engine/application_helper.rb

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

It works, but once, when I restarted dev server, it throws me uninitialized constant ActionView::Helpers::ApplicationHelper, but I can't reproduce this exception.

EDIT

Removed this include and made this one:

lib/my_engine/engine.rb (it's inside engine)

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


来源:https://stackoverflow.com/questions/8028021/helpers-in-rails-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!