Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-reload changes in a Rails Engine?

I have a Rails 4.1.0 mountable engine. In the engine's application_helper.rb:

module MyEngine
  module ApplicationHelper    
    def test123
      "test123"
    end    
  end
end

The method is in the dummy app's view general/index.html.erb view:

%<= test123 %>

This works. However, when I change the string returned by def test123 and refresh the browser, the new string is not displayed.

Of course, restarting the web server in the dummy app shows the new string.

So the question is, how to reload the engine's files without having to restart the web server?

PS. I am preferably looking for a way to do this using Rails itself, or a specific gem that solves this problem (but not the generic gems like Guard, Spork etc. although if all else fails, I will consider those too.)

PPS. There are similar questions on SO, but I have tried them all (even though they are for Rails 2.x, 3.x), and they have not worked for me.

like image 740
Zabba Avatar asked Nov 24 '13 07:11

Zabba


People also ask

How does autoload work in Rails?

Rails automatically reloads classes and modules if application files in the autoload paths change. More precisely, if the web server is running and application files have been modified, Rails unloads all autoloaded constants managed by the main autoloader just before the next request is processed.

What is autoload in Ruby?

The Module#autoload method registers a file path to be loaded the first time that a specified module or class is accessed in the namespace of the calling module or class.

What is a Rails engine?

1 What are Engines? Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine .


1 Answers

You should explicitly require dependent helpers:

# engines/my_engine/app/controllers/my_engine/application_controller.rb

require_dependency "my_engine/application_helper"  # This is a key point!

module MyEngine
  class ApplicationController < ::ApplicationController
    helper ApplicationHelper
    ...
like image 170
907th Avatar answered Oct 22 '22 03:10

907th