ruby-on-railsrailtie

NameError: uninitialized constant GemName::Rails::Railtie


I'm creating a gem to encapslate a chunk of functionality from an application. Them gem essentially runs a rake task, but when I run the task with bundle exec rake:assets:precompile, I get the following error

rake aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'gem-name'.
/Users/tonyedwardspz/myprojects/westcornwallevents/config/application.rb:8:in `<top (required)>'
/Users/tonyedwardspz/myprojects/westcornwallevents/Rakefile:4:in `require'
/Users/tonyedwardspz/myprojects/westcornwallevents/Rakefile:4:in `<top (required)>'
NameError: uninitialized constant GemName::Rails::Railtie
/Users/tonyedwardspz/myprojects/westcornwallevents/config/application.rb:8:in `<top (required)>'
/Users/tonyedwardspz/myprojects/westcornwallevents/Rakefile:4:in `require'
/Users/tonyedwardspz/myprojects/westcornwallevents/Rakefile:4:in `<top (required)>'

The code for the module related to railtie is:

require 'rails'

module GemName
  module Rails
    class Railtie < Rails::Railtie
      railtie_name :gem_name

      rake_tasks do
        load "tasks/gem_name.rake"
      end
    end
  end
end

Any ideas why I'm getting this error?


Solution

  • The error is in the syntax of the class definition. Updating the code as following will allow the rake task to run.

    class Railtie < Rails::Railtie
    

    to

    class Railtie < ::Rails::Railtie