我正在创建一个gem来封装应用程序中的一块功能。Them-gem本质上运行一个rake任务,但当我使用bundle exec rake:assets:precompile
运行该任务时,我会得到以下错误
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)>'
与枕木相关的模块代码为:
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
你知道我为什么会犯这个错误吗?
错误出现在类定义的语法中。按照以下方式更新代码将允许rake任务运行。
class Railtie < Rails::Railtie
至
class Railtie < ::Rails::Railtie