8

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.

tshepang
  • 11,360
  • 21
  • 88
  • 132
Markus
  • 2,446
  • 3
  • 25
  • 35

2 Answers2

10

Put this code in engine.rb:

config.to_prepare do
  ApplicationController.helper(MyEngineHelper)
end
Brad Werth
  • 16,916
  • 9
  • 62
  • 86
3

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
Alex Fedoseev
  • 965
  • 10
  • 18
  • 1
    I had to change the .helper line in Rails 4.2 to this: ApplicationController.helper(::ApplicationHelper) ... That might even have worked with your first solution. – Allen Jul 01 '15 at 16:15
  • I follow this solution and application_helper works fine, but when I run `rails console`, I got "uninitialized constant ActionView::Helpers::ApplicationHelper (NameError)". Does someone knows the reason? – Bater Chen Dec 01 '21 at 06:48