Reputation: 43666
I have written a generator which creates the following ruby file and folder:
app/tests/test.rb
in the test.rb
file I have a Test
class which looks like this:
class Test < MyCustomModule::MyCustomClass::Base
...
end
Now, I want to use its functionality in one of the show.html.erb
files creating new instance like this:
Test.new(...).render(...).html_safe
but I am getting the following error:
uninitialized constant MyCustomModule::MyCustomClass::Base
I have use the following answer to link my gem and my rails application. It seems to work as I am able to use the generator, but the gem module and class are not seen in the rails application.
Could anyone tell how to fix this issue?
I have try to follow the tips posted here but still nothing changed:
config.autoload_paths += Dir["#{config.root}/lib/**/"]
in application.rb
fileCarrierWave
gem, so the naming should be correctI try to disable config.threadsafe!
but it is already disabled since config.cache_classes
and config.eager_load
are set to false in development
DEPRECATION WARNING: config.threadsafe! is deprecated. Rails applications behave by default as thread safe in production as long as config.cache_classes and config.eager_load are set to true.
Also, looking at adding-asset-to-your-gems rails documentation, it is said that:
A good example of this is the jquery-rails gem which comes with Rails as the standard JavaScript library gem. This gem contains an engine class which inherits from Rails::Engine. By doing this, Rails is informed that the directory for this gem may contain assets and the app/assets, lib/assets and vendor/assets directories of this engine are added to the search path of Sprockets.
So, I have done this, and put my model class file in assets
folder, but the result is the same.
The following screenshots demonstrate my real case:
The screenshot below displays my gem file structure
Here you can see how I am loading the gem in my Rails application Gemfile
:
gem 'thumbnail_hover_effect', '0.0.3', github: 'thumbnail_hover_effec/thumbnail_hover_effec', branch: 'master'
Then I am using the gem generator a ruby file with a cutstom name in app/thumbnails/test.rb
folder with the following code:
class Test < ThumbnailHoverEffect::Image::Base
...
end
and trying to use the Test
class gives me uninitialized constant ThumbnailHoverEffect::Image::Base
error.
Back in the gem files, these are how the thumbnail_hover_effect
file looks like
require 'thumbnail_hover_effect/version'
require 'thumbnail_hover_effect/engine'
require 'thumbnail_hover_effect/image'
module ThumbnailHoverEffect
# Your code goes here...
end
and hoe the image
file looks like:
module ThumbnailHoverEffect
#
class Image
...
end
end
Upvotes: 0
Views: 1555
Reputation: 17246
From what you've posted here there is no ThumbnailHoverEffect::Image::Base
defined. Rails autoloading conventions (which you should not be depending on a gem btw, more on that later) would be looking for this file in thumbnail_hover_effect/image/base.rb
, but the directory structure you printed does not have that. Of course you could define the class in thumbnail_hover_effect/image.rb
and it would work, but the abridged snippet you posted does not show that. So where is ThumbnailHoverEffect::Image::Base
defined?
If it's in thumbnail_hover_effect/image/base.rb
then that would indicate the file is not being loaded. You can sanity check this by putting a puts 'loading this stupid file'
at the top of thumbnail_hover_effect/image/base.rb
. That will allow you to bisect the problem by seeing whether there is a problem with your definition of the class, or whether the problem is with loading the proper files. Debugging is all about bisecting the problem.
Upvotes: 1