无法加载lib目录模块 - 未初始化的常量 - rails 2到rails 3升级

时间:2022-05-10 11:00:37

I'm currently migrating an application in rails v2 to v3

我目前正在将rails v2中的应用程序迁移到v3

In my lib/ i've some modules in subdirectories, for example, i've the lib/search/host_search.rb

在我的lib / i中有子目录中的一些模块,例如,我有lib / search / host_search.rb

with a

  module HostSearch
    def do_search(args)
       #...
    end
  end

then I need to use it in a controller named Discovery::HostController < ApplicationController :

然后我需要在名为Discovery :: HostController 的控制器中使用它:

def search_results
   output = HostSearch.do_search(:search_string => @search_string, 
     :page => params[:page],
     :user => @current_user)
   #...
end

But have I get:

但是我得到了:

uninitialized constant Discovery::HostController::HostSearch

..I tried to put this lines in application.rb but it doesn't work..

..我试图将这些行放在application.rb中,但它不起作用..

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

2 个解决方案

#1


5  

I found that moving the module to the lib folder or explicitly including the folder to load worked, in your case config.autoload_paths += %W(#{config.root}/lib/search)

我发现将模块移动到lib文件夹或显式包含要加载的文件夹,在你的情况下config.autoload_paths + =%W(#{config.root} / lib / search)

I think there's something syntaxical that we are missing. Another thing is that if you don't want to mess with the application.rb file, require the file, which if I remember, takes the file path from the lib folder eg: search/host_search <- check that.

我认为我们缺少一些语法上的东西。另一件事是,如果你不想搞乱application.rb文件,请求文件,如果我记得的话,从lib文件夹获取文件路径,例如:search / host_search < - 检查。

#2


0  

I think if you put the HostSearch module under a search subdir, (ie in lib/search/host_search.rb), then you need to namespace it:

我想如果你将HostSearch模块置于搜索子目录下(即在lib / search / host_search.rb中),那么你需要命名它:

module Search
  module HostSearch
  end
end

If you don't want to namespace it, you can should move the file into the lib root: lib/host_search.rb.

如果您不想命名它,可以将文件移动到lib root:lib / host_search.rb。

See also: https://*.com/a/19650564/514483

另见:https://*.com/a/19650564/514483

#1


5  

I found that moving the module to the lib folder or explicitly including the folder to load worked, in your case config.autoload_paths += %W(#{config.root}/lib/search)

我发现将模块移动到lib文件夹或显式包含要加载的文件夹,在你的情况下config.autoload_paths + =%W(#{config.root} / lib / search)

I think there's something syntaxical that we are missing. Another thing is that if you don't want to mess with the application.rb file, require the file, which if I remember, takes the file path from the lib folder eg: search/host_search <- check that.

我认为我们缺少一些语法上的东西。另一件事是,如果你不想搞乱application.rb文件,请求文件,如果我记得的话,从lib文件夹获取文件路径,例如:search / host_search < - 检查。

#2


0  

I think if you put the HostSearch module under a search subdir, (ie in lib/search/host_search.rb), then you need to namespace it:

我想如果你将HostSearch模块置于搜索子目录下(即在lib / search / host_search.rb中),那么你需要命名它:

module Search
  module HostSearch
  end
end

If you don't want to namespace it, you can should move the file into the lib root: lib/host_search.rb.

如果您不想命名它,可以将文件移动到lib root:lib / host_search.rb。

See also: https://*.com/a/19650564/514483

另见:https://*.com/a/19650564/514483