I new to rails. I have a setup in the lib directory like so:
lib/ blog/ core/ search/ base.rb
The base.rb defines the Base class as well:
module Blog module Core module Search class Base attr_accessor :properties def initialize(params) @properties = {} end end end end end
I have the following code in my application.rb
config.autoload_paths += Dir["#{config.root}/lib/**/"]
When I include it in posts controller I get following errors:
LoadError in PostsController#index Expected /home/usr/code/blog/lib/blog/core/search/base.rb to define Base
Any idea? I'm using rails 3.2.5 with RVM. Thank you for every advice.
UPDATED: Added my full stack:
Started GET "/admin/posts" for 127.0.0.1 at 2012-06-08 21:06:18 +0800 LoadError (Expected /home/usr/code/blog/lib/blog/core/search/base.rb to define Base): app/controllers/admin/base_controller.rb:5:in `<top (required)>' app/controllers/admin/posts_controller.rb:6:in `<top (required)>' Rendered /home/usr/.rvm/gems/[email protected]/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.9ms) Rendered /home/usr/.rvm/gems/[email protected]/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.6ms) Rendered /home/usr/.rvm/gems/[email protected]/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (7.0ms)
In Ruby, a class is an object that defines a blueprint to create other objects. Classes define which methods are available on any instance of that class. Defining a method inside a class creates an instance method on that class. Any future instance of that class will have that method available.
No, you can't instantiate a module inside a class. Module are static objects that are instantiated before time 0 during elaboration.
To access the instance method defined inside the module, the user has to include the module inside a class and then use the class instance to access that method. Below example illustrate this concept clearly. The user can use the module inside the class by using include keyword.
I had the same problem. It comes from the fact that you try to load /lib/blog/core/search/base.rb directly in application.rb with /lib/**/
Error I had:
Expected /[...]/myapp/lib/durative/base.rb to define Base (LoadError)
Directory structure:
lib/ --durative/ --base.rb
base.rb:
module Durative class Base def initialize(config) @config = {} end #... end end
application.rb:
config.autoload_paths += Dir["#{config.root}/lib/**/"]
Directory structure:
lib/ --durative.rb **(added)** --durative/ --base.rb
durative.rb:
require 'durative/base'
base.rb (no change)
application.rb (changed):
config.autoload_paths += Dir["#{config.root}/lib/"]
Tell us if it worked for you too.
I had the same issue. The problem was because I was including subdirs without including their parent lib dir:
# did not work config.autoload_paths += %W(#{config.root}/lib/foo)
and
# in lib/foo/my_class.rb module Foo class MyClass end end
Foo::MyClass would return Expected to define MyClass
adding the lib dir to config.autoload_paths fixes the problem
# worked config.autoload_paths += %W(#{config.root}/lib #{config.root}/lib/foo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With