[英]Ruby on Rails Inflection error - uninitialized constant
I am using Ruby on Rails to create a website for a game I play.我正在使用 Ruby on Rails 为我玩的游戏创建一个网站。
I have a User
model and a Starbase
model.我有一个User
模型和一个Starbase
模型。 The relationship I am trying to setup is like so我试图建立的关系是这样的
class User < ActiveRecord::Base
has_many :starbases
end
class Starbase < ActiveRecord::Base
belongs_to :user
end
However when I open script/console and try to access the users starbases it gives me an error: NameError: uninitialized constant User::Starbasis
.但是,当我打开脚本/控制台并尝试访问用户 starbases 时,它给了我一个错误: NameError: uninitialized constant User::Starbasis
。
It seems as if it is a problem with inflection and rails is not pluralizing starbase correct.似乎这是拐点的问题,而 rails 并没有使 starbase 复数正确。
I have tried adding this to the inflections.rb in the intializers folder:我尝试将其添加到 intializers 文件夹中的 inflections.rb 中:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural 'starbase', 'starbases'
end
but it still does not solve the issue.但它仍然不能解决问题。 Could anyone give advice on how to get this working?任何人都可以就如何使这项工作提供建议吗?
Have you tried adding a line for the inverse inflection (ie 'singular'):您是否尝试过为逆屈折添加一条线(即“单数”):
inflect.singular "starbases", "starbase"
I tried your example in my console and it was the singularization that caused problems, not the other way around.我在控制台中尝试了您的示例,导致问题的是单数化,而不是相反。 I'm not sure if this fixes other issues (like routes), but it should fix the simple stuff (I think).我不确定这是否解决了其他问题(如路线),但它应该解决简单的问题(我认为)。
Little trick i picked up to double check how Active Support might singularize, or pluralize my Class names, and/or Module names.我学会了一个小技巧来仔细检查 Active Support 如何使我的类名称和/或模块名称单数化或复数化。
have your rails app server running and in a new tab enter into your rails console by typing rails console
.让您的 Rails 应用程序服务器运行并在新选项卡中通过键入rails console
进入您的 Rails 控制台。 In there you can easily double check for the correct style for your names.在那里,您可以轻松地仔细检查您姓名的正确样式。
long way ActiveSupport::Inflector.pluralize "fish"
# => "fish"
很长的路ActiveSupport::Inflector.pluralize "fish"
# => "fish"
short way "fish".pluralize
# => "fish"
捷径"fish".pluralize
# => "fish"
You can find more examples here您可以在此处找到更多示例
https://github.com/rails/rails/blob/master/activesupport/test/inflector_test_cases.rb https://github.com/rails/rails/blob/master/activesupport/test/inflector_test_cases.rb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.