Markus
Markus

Reputation: 2564

Helpers in Rails engine

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.

Upvotes: 8

Views: 6176

Answers (3)

Gabriel Guérin
Gabriel Guérin

Reputation: 468

Adding this just in case:

I had the same problem using the Administrate gem with Rails 7. I wanted to access my main app helper modules.

Simply adding helper all_helpers_from_path 'app/helpers' in Admin::ApplicationController solved this. You can find the official documentation here.

My file now looks like this:

module Admin
  class ApplicationController < Administrate::ApplicationController
    before_action :authenticate_admin_user!
    helper all_helpers_from_path "app/helpers"
  end
end

I found the answer here.

Upvotes: 0

Alex Fedoseev
Alex Fedoseev

Reputation: 1215

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

Upvotes: 3

Brad Werth
Brad Werth

Reputation: 17647

Put this code in engine.rb:

config.to_prepare do
  ApplicationController.helper(MyEngineHelper)
end

Upvotes: 10

Related Questions