简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM