Rails Engines: Helpers only are reloaded when restarting the server

时光总嘲笑我的痴心妄想 提交于 2019-12-05 00:13:16

It appears that you may be barking up the wrong tree with Engines. If you're trying to simply achieve separation of concerns, you probably just want to make some plain old ruby classes and stick them in lib/ (in an organized way of course).

An Engine would be developed separately from your 'current' project at likely brought in through a gem. Changes in included gems would necessitate restarting your server AFAIK.

hraynaud

If you need code from your engine reloaded on every request you need to place it in the to_prepare block of the engines intialization code

module IqList
  class Engine < ::Rails::Engine
    config.to_prepare do
     ApplicationController.helper(IqListHelper)
    end
  end
end

Code in the to_prepare block is guaranteed to run once in production and every time in development.

see the rails guides as well as What does this Rails Engine code mean: config.to_prepare &method(:activate).to_proc

and

http://robots.thoughtbot.com/tips-for-writing-your-own-rails-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!