当我尝试生成脚手架时出现 Rails 错误
Posted
技术标签:
【中文标题】当我尝试生成脚手架时出现 Rails 错误【英文标题】:Rails error when I try generate a scaffold 【发布时间】:2018-06-22 06:52:01 【问题描述】:对不起我的英语我是法国人;)
当我尝试使用 rails g scaffold (data info)
生成脚手架时,我遇到了一个错误,我从来没有看到过这个
rails g scaffold title:string language:string engine:string little_description:string description:text on_production:boolean game_type:string platform:string dimension:string
invoke active_record
Traceback (most recent call last):
34: from bin/rails:4:in '<main>'
33: from bin/rails:4:in 'require'
32: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in '<top (required)>'
31: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/command.rb:44:in 'invoke'
30: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in 'perform'
29: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor.rb:387:in 'dispatch'
28: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in 'invoke_command'
27: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/command.rb:27:in 'run'
26: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/generate/generate_command.rb:24:in 'perform'
25: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/generators.rb:269:in 'invoke'
24: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/base.rb:466:in 'start'
23: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/group.rb:232:in 'dispatch'
22: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in 'invoke_all'
21: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in 'map'
20: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in 'each'
19: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in 'block in invoke_all'
18: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in 'invoke_command'
17: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/command.rb:27:in 'run'
16: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/group.rb:133:in '_invoke_from_option_orm'
15: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/group.rb:266:in '_invoke_for_class_method'
14: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/shell.rb:68:in 'with_padding'
13: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/group.rb:277:in 'block in _invoke_for_class_method'
12: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:115:in 'invoke'
11: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/group.rb:232:in 'dispatch'
10: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in 'invoke_all'
9: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in 'map'
8: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in 'each'
7: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in 'block in invoke_all'
6: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in 'invoke_command'
5: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/command.rb:27:in 'run'
4: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/generators/named_base.rb:236:in 'block in check_class_collision'
3: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/generators/base.rb:249:in 'class_collisions'
2: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/generators/base.rb:249:in 'each'
1: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/generators/base.rb:258:in 'block in class_collisions'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/generators/base.rb:258:in 'const_defined?': wrong constant name Title:string (NameError)
你知道解决方案是什么 我认为问题出在主动记录上。
谢谢
【问题讨论】:
【参考方案1】:您收到错误是因为您错过了生成命令中的模型名称。你的脚手架命令应该是
rails g scaffold <ModelName> title:string language:string engine:string little_description:string description:text on_production:boolean game_type:string platform:string dimension:string
如果你的模型名称是Product
,脚手架应该是这样的
rails g scaffold Product title:string language:string engine:string little_description:string description:text on_production:boolean game_type:string platform:string dimension:string
【讨论】:
【参考方案2】:您生成脚手架的语法不正确。
Rails 中的脚手架是一套完整的模型、该模型的数据库迁移、用于操作它的控制器、用于查看和操作数据的视图,以及用于上述各项的测试套件。
您在这里缺少模型名称
语法是rails g scaffold model_name data_hash
前任
rails g scaffold Product title:string language:string engine:string little_description:string description:text on_production:boolean game_type:string platform:string dimension:string
【讨论】:
以上是关于当我尝试生成脚手架时出现 Rails 错误的主要内容,如果未能解决你的问题,请参考以下文章
Rails 3.1.10 中脚手架生成的视图规范中缺少部分错误
如何更新脚手架生成的 MVC + 路由,用于在 Rails 中具有 2 个 belongs_to 关系的嵌套资源