L
L
Lesha Fedoseev2014-02-15 14:09:59
Ruby on Rails
Lesha Fedoseev, 2014-02-15 14:09:59

Using host application helpers in mountable engine views

I have a mountable engine that mounts to the main application.
In the main application, directly in ApplicationHelperthere is a very simple helper edit_link(path): you give it a url, it returns the html code of the link.
The engine has a couple of controllers with views (interfaces) in which I want to use this helper.
By default, Rails doesn't let me do this: I've tried using both simple edit_link(path)and main_app.edit_link(path)(this works with routes, but not with helpers).
Then I decided to do this:
app/helpers/my_engine/application_helper.rb

module MyEngine
  module ApplicationHelper
    include ActionView::Helpers::ApplicationHelper
  end
end

It worked, though. When I restarted the dev server, the first request to the engine interface caught an exception:
uninitialized constant ActionView::Helpers::ApplicationHelper

I made a request to the main application controller and returned to the engine interface - it works again. Restarted the server again and made the first request to the engine interface - it works. And I can’t catch uninitialized constantthe exception anymore.
I would like to understand what it was and whether it is possible to include helpers of the main application in the views of the mountable engine using this method.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lesha Fedoseev, 2014-02-15
@alexfedoseev

In general, it seems to work like this:
lib/my_engine/engine.rb (this is inside the engine)

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

PS He includekilled accordingly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question