简体   繁体   English

在Hanami 1.0.0上运行的WEBrick显示由于错误的变形而导致的错误

[英]WEBrick run on hanami 1.0.0 shows errors because of wrong inflection

I generated model with hanami generate model stimulus . 我用hanami generate model stimulus生成了模型, hanami generate model stimulus Then I fixed "stimuluses" to "stimuli" in the migration file name and inside, the table name. 然后,我在迁移文件名以及表名的内部将“刺激”固定为“刺激”。

Everytime I load a page I get this error in the server console window: 每次加载页面时,都会在服务器控制台窗口中收到此错误:

[ROM::Relation[Stimuluses]] failed to infer schema. [ROM :: Relation [Stimuluses]]无法推断架构。 Make sure tables exist before ROM container is set up. 在设置ROM容器之前,请确保存在表。 This may also happen when your migration tasks load ROM container, which is not needed for migrations as only the connection is required (schema parsing returned no columns, table "stimuluses" probably doesn't exist) 当您的迁移任务加载ROM容器时,也可能会发生这种情况,因为只需要连接,迁移就不需要此容器(模式解析不返回任何列,表“刺激”可能不存在)

I looked into the libraries and found that this functionality has Inflecto library. 我查看了这些库,发现此功能具有Inflecto库。 Then I tried both adding to hanami project this: 然后,我尝试将两者都添加到hanami项目中:

# /config/initializers/inflecto.rb
require 'inflecto'

Inflecto.inflections do |inflect|
  inflect.irregular('stimulus', 'stimuli')
end

And editing the defualt library file: 并编辑默认库文件:

# gems/inflecto-0.0.2/lib/inflecto/defaults.rb
Inflecto.inflections do |inflect|
  ...
  inflect.irregular('stimulus', 'stimuli')
  ...
end

But the message is still there after restarting the server. 但是,重新启动服务器后,该消息仍然存在。

Is this something I should solve and if yes, how to do this? 这是我应该解决的问题吗?如果是,该怎么做?

EDIT: 编辑:

Also tried: 还尝试了:

# /config/initializers/inflector.rb
require 'hanami/utils/inflector'

Hanami::Utils::Inflector.inflections do
  exception 'stimulus', 'stimuli'
end

I'm assuming we are talking about Hanami v1.0.0, right? 我假设我们正在谈论Hanami v1.0.0,对吗?

You nearly succeeded. 您几乎成功了。 What hit you is that initializers seem to be not loaded when executing hanami commands and maybe a bug in code reloading . 令您震惊的是,执行hanami命令时似乎未加载初始化程序,并且可能会在代码重新加载时出现错误 So instead of an initializer put it into a file that gets loaded when hanami commands are executed or require the initializer file in such a place. 因此,代替初始化器,将其放到执行hanami命令时加载的文件中,或者在此类位置需要初始化器文件时即可。 Eg, 例如,

# config/initializers/inflections.rb
require 'hanami/utils/inflector'

Hanami::Utils::Inflector.inflections do
  exception 'stimulus', 'stimuli'
end

and then in your environment file 然后在您的环境文件中

# config/environment.rb
# ...
require_relative 'initializers/inflections.rb'
# ...

I'm not sure if that is a good place to put custom inflection rules, but at least it works. 我不确定这是否是放置自定义拐点规则的好地方,但至少它能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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