Rails Engine with Isolated Namespace Sharing Layout

I have a Rails Engine that I would like to share with a container. I would like to support all the URL assistants from the main application layout to make integration trivial. That is, to support layouts with helpers from the container application:

= link_to "Signup", new_user_path = link_to "Login", new_user_path ... 

It causes:

undefined local variable or method `new_user_path 'for # <#: 0x007f9bf9a4a168>

I can fix this by changing application.html (in the container application) to:

 = link_to "Signup", main_app.new_user_path = link_to "Login", main_app.new_user_path 

But the goal is to ensure that the integration of the engine does not require users to change the existing functioning of application.html .

I believe that I can also fix errors by removing isolate_namespace Example from lib/example/engine.rb , but this breaks almost everything in the engine.

In any case, in order to allow the helpers of the container application and explicitly leak my helpers of my engines to avoid conflicts? (i.e. using example.root_path instead of root_path )?

+8
ruby-on-rails rails-engines
source share
1 answer

Have a look at this: https://github.com/rails/rails/blob/a690207700d92a1e24712c95114a2931d6197985/actionpack/lib/abstract_controller/helpers.rb#L108

You can include your helpers from your engine in the host application.

 module Blargh class Engine < ::Rails::Engine isolate_namespace Blargh config.to_prepare do # application helper ApplicationController.helper(Blargh::ApplicationHelper) # any other helper end end end 

Thus, you can easily use your assistant in your rail host. Of course, there are no real names, so if the user of your engine calls the new helper method the same as your helper method, it will collide.

Does this answer your question?

0
source share

All Articles