![](/img/trans.png)
[英]Rails - wrong number of arguments(1 for 0) for a newly generated Rails app?
[英]Can't mount newly generated Rails engine in an app
我有一个复杂的 Rails 应用程序,我想将一些核心功能提取到一个引擎中,以便我可以在其他 Rails 应用程序中重用模型等。
我一直在关注引擎的官方文档( https://guides.rubyonrails.org/engines.html )。 我能够在应用程序中创建一个新引擎并生成一些测试模型
> rails plugin new testengine --mountable
testengine> rails generate model Test
这是.gemspec
require_relative "lib/testengine/version"
Gem::Specification.new do |spec|
spec.name = "testengine"
spec.version = Testengine::VERSION
spec.authors = ["Me"]
spec.summary = "testengine"
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
spec.files = Dir["{app,config,db,lib}/**/*", "Rakefile", "README.md"]
spec.add_dependency "rails", "~> 6.1.4"
end
我控制台进入testengine中的测试虚拟轨道应用程序,我可以在Testengine::Test
找到我的新 model,没问题。 到目前为止,一切都很好。
现在我进入第4.1 节安装引擎。 我通过Gemfile文件添加引擎(事实上,由于上面的 rails 生成器,这已经为我完成了)。
gem 'testengine', path: 'testengine'
然后我安装我的宝石没有问题。
> bundle install
...
Using testengine 0.1.0 from source at `testengine`
...
我控制台进入主应用程序,我可以找到Testengine
和Testengine::VERSION
但不是Testengine::Engine
或Testengine::Test
。
进一步阅读文档说您需要将此行添加到config/routes.rb
mount Testengine::Engine, at: "/testengine"
我这样做了,现在 Rails 应用程序甚至无法启动
config/routes.rb:3:in `block in <top (required)>': uninitialized constant Testengine::Engine (NameError)
我错过了什么?
我将回答我的问题,以帮助其他可能犯同样错误的人。 就我而言, gem 'testengine', path: 'testengine'
被埋在一组 gem 中,例如
group :test do
...
end
我想我对 rails 如何从组中加载 gem 感到困惑,并且错过了有关组包含的细节。 似乎虽然它会在bundle install
期间显示在列表中,并自动加载一些基本项目,例如Testengine::VERSION
它不会自动加载所有内容,除非您正在运行与组同名的环境。 事后看来,这似乎有点明显。 学过的知识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.