I put the class "Hello" in a file named 'hello.rb', placed the file in the 'lib',
and 'autoload' the 'lib' directory by putting in the following line in the 'Application.rb' file:
It worked like a charm! I can invoke the class method of my "Hello" class in Rails Console! Hooary!config.autoload_paths += %W(#{config.root}/lib)
Then I changed the class to "HelloWorld", and I renamed my file to 'hello_world.rb'.
Theoretically, it should just work. But it didn't!
After pulling off a few hair from my almost hairless head, I found an interesting article by Bill Harding, and the suggested solution works for me is to force load the new file through initializers.
I created a new file 'extensions.rb' in 'config/initializers',
then put the following line into 'extensions.rb':
It worked! Well, it's not the way I really wanted it, but I'm happy it worked for me for now.require File.join(Rails.root, "lib", "hello_world")
Note:
The 'Autoload' configuration in the 'Application.rb' file is not necessary any more in this case.
Hope it helps.
No comments:
Post a Comment