Catalog modules cant load lib - uninitialized constant - rails 2 for rails 3 updates

I am currently porting an application in rails v2 to v3

In my lib/ I have some modules in subdirectories, for example, I have lib/search/host_search.rb

from

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

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

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

But do I get:

 uninitialized constant Discovery::HostController::HostSearch 

.. I tried putting these lines in application.rb, but it does not work.

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

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

I think there is something syntactical that we are missing. Another thing is that if you do not want to contact the application.rb file, you need a file that, if I remember, takes the path to the file from the lib folder, for example: search / host_search <- check this.

+5
source

I think if you put the HostSearch module under the search subdirectory (i.e. in lib/search/host_search.rb ), then you need to skip it:

 module Search module HostSearch end end 

If you do not want the namespace to be moved, you can move the file to the root directory lib: lib/host_search.rb .

See also: fooobar.com/questions/31076 / ...

0
source

Source: https://habr.com/ru/post/901815/


All Articles