如何在 Rails 引擎中自动重新加载更改? [英] How to auto-reload changes in a Rails Engine?

查看:46
本文介绍了如何在 Rails 引擎中自动重新加载更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rails 4.1.0 可安装引擎.在引擎的 application_helper.rb 中:

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

方法在虚拟应用的视图general/index.html.erb视图中:

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

%<= test123 %>

这有效.但是,当我更改 def test123 返回的字符串并刷新浏览器时,新字符串没有显示.

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

当然,在虚拟应用程序中重新启动 Web 服务器会显示新字符串.

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?

附注.我最好寻找一种使用 Rails 本身或解决此问题的特定 gem 的方法(但不是像 Guard、Spork 等通用 gem,尽管如果其他方法都失败了,我也会考虑这些.)

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.SO 上也有类似的问题,但我都试过了(即使它们适用于 Rails 2.x、3.x),但它们对我不起作用.

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.

推荐答案

你应该明确地要求依赖的助手:

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
    ...

这篇关于如何在 Rails 引擎中自动重新加载更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆