Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant load lib directory modules - uninitialized constant - rails 2 to rails 3 upgrade

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

In my lib/ i've some modules in subdirectories, for example, i've the 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 :

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

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
like image 405
Orange Avatar asked Nov 18 '11 15:11

Orange


1 Answers

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)

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.

like image 200
rkamun1 Avatar answered Oct 04 '22 18:10

rkamun1