【问题标题】:Rails - Google Translate API : ArgumentError: unknown keyword: projectRails - Google Translate API:ArgumentError:未知关键字:项目
【发布时间】:2019-11-18 14:18:10
【问题描述】:

我有这段代码在我的使用谷歌翻译 API 的 rails 应用程序上运行,但在我最后一次提交 heroku 时出现错误

ArgumentError: unknown keyword: project 

我的谷歌分析似乎也不起作用。不知道是什么导致了错误。也许你有提示?

translate = Google::Cloud::Translate.new project: "my_project"
description_translation = translate.translate params[:description], to: 'en'
update_attribute(:description, description_translation)

【问题讨论】:

  • 你在为 api 使用 gem 吗?如果有,是哪一个?
  • @lacostenycoder 是 gem 'google-cloud' & gem 'google-cloud-translate'

标签: ruby-on-rails ruby google-api google-translate


【解决方案1】:

projectproject_id 的别名。从库的版本 1.1.0 开始,它是 deprecated

您的代码可能停止工作,因为您将库更新为version 2.0.0(或更高版本) - 因为here, in this PR,对project 参数的支持已完全放弃。

要修复此错误,只需将 project 重命名为 project_id

还要注意任何其他潜在的破坏性更改here, in the CHANGELOG。如该链接所示,您可能希望暂时使用version: :v2 来帮助缓解迁移,如果有进一步的复杂情况。

【讨论】:

  • 我仍然有同样的错误 ArgumentError: unknown keyword: project_id 谷歌翻译文档说它应该是 project 而不是 project_id。 cloud.google.com/translate/docs/quickstart-client-libraries
  • 我没有看到该文档在哪里说明了 projectproject_id 的相关内容?不管怎样,该文档似乎是针对 v2 API 的——我在上面已经解释了如何希望保持向后兼容的支持,而无需降级库。该网站还展示了如何继续使用v2 API,正如我上面所说的:cloud.google.com/translate/docs/reference/libraries/v2/ruby
  • 实际上再深入一点,我发现v3 API 的语法发生了显着变化。你可以在文档中看到here的用法。
  • 非常感谢它与 translate = Google::Cloud::Translate.new version: :v2, project_id: "my_project" 我将阅读 V3 的文档!非常感谢您的宝贵帮助!
  • ....或者,如果所有其他方法都失败了,您可以降级库。您从未真正声明(也未确认)该错误是由库升级引起的;这是我花时间挖掘文档后自己的假设。但对于未来的读者来说,这可能是一条非常重要的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多