【问题标题】:Active Record unexpected i18n errorActive Record 意外 i18n 错误
【发布时间】:2017-08-14 15:01:29
【问题描述】:

我正在构建一个使用活动记录但不使用 Rails 的 Ruby 项目。在我的一项测试中,我正在尝试以下内容:

it "fails with no driver name" do
  command = "Driver"
  expect {command_file.process_driver command}.to raise_error(ActiveRecord::RecordInvalid)
end

这是我试图调用的方法

def process_driver command
  driver_name = command.split[1]
  Driver.create! :name => driver_name
end

我希望将:name => nil 传递给Driver.create!,这应该会抛出RecordInvalid,但我会得到I18n::InvalidLocaleData。这是回溯

expected ActiveRecord::RecordInvalid, got #<I18n::InvalidLocaleData: can not load translations from /Users/me/.rbenv/versions/2.3.1/lib/r...ems/activesupport-5.1.3/lib/active_support/locale/en.yml: expects it to return a hash, but does not> with backtrace:
  # ./command_file.rb:81:in `process_driver'
  # ./command_file.rb:63:in `block in process'
  # ./command_file.rb:51:in `each'
  # ./command_file.rb:51:in `each_with_index'
  # ./command_file.rb:51:in `process'
  # ./spec/command_file_spec.rb:60:in `block (5 levels) in <top (required)>'
  # ./spec/command_file_spec.rb:60:in `block (4 levels) in <top (required)>'
  # ./spec/spec_helper.rb:75:in `block (3 levels) in <top (required)>'
  # ./spec/spec_helper.rb:74:in `block (2 levels) in <top (required)>'

这是我的Gemfile

source 'https://rubygems.org'

gem 'sqlite3', '~> 1.3', '>= 1.3.13'
gem 'activerecord', '~> 5.1', '>= 5.1.3'
gem 'pry', '~> 0.10.4'
gem 'rspec', '~> 3.6'
gem 'factory_girl', '~> 4.5'

group :test do
  gem 'database_cleaner'
end

我没有自己的语言环境文件。

知道发生了什么吗?我没有在这个项目中尝试任何形式的翻译。我也不明白为什么active_support 提供的语言环境文件会失败。如果可能的话,我很乐意以某种方式简单地禁用 i18n,但我不知道该怎么做。任何想法是什么问题?

【问题讨论】:

  • 您有任何.yml 或配置文件吗?你能附上.gemfile吗?
  • @MaxL。我没有.yml 或配置文件。我已将我的Gemfile 添加到问题中
  • 您的config/locales 文件夹中有任何内容吗?可能是空的en.yml 文件?
  • @SergioTulentsev 我没有config/locales 文件夹。我什至不打算进行国际化。
  • 您能否验证您的计算机上是否存在 {your-ruby-gempath}/active_support-5.1.3/lib/active_support/locale/en.yml 文件?您是否使用 rb env 切换了 ruby​​ 版本 - 您是否需要使用当前版本的 ruby​​ 运行 bundle install 才能安装 active_support?或者切换回正确的 rb env ruby​​ 版本?

标签: ruby activerecord internationalization activesupport


【解决方案1】:

出于某种原因,:en 未被设置为我的默认语言环境。我在我的spec_helper.rb 中通过添加I18n.default_locale = 'en' 修复了这个问题:

I18n.default_locale = 'en' # <--- add this line

RSpec.configure do |config|
    # config here...
end

我意识到这并不能解决更大的问题,即为什么来自 active_support 的语言环境文件没有加载,但我的挑战只是让错误消失,而不是使用 i18n

【讨论】:

    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多