加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Mongoid字段,随后添加了本地化

发布时间:2020-12-17 03:06:26 所属栏目:百科 来源:网络整理
导读:我稍后在添加本地化时有什么需要考虑的事项吗? 从 ????类文章 ????field:title,type:String 至 ????类文章 ????field:title,type:String,localize:true 我对我的文章模型有内容,并希望随后添加本地化. 现在我发现有时内容会显示为哈希 {"en"="asdfasdf
我稍后在添加本地化时有什么需要考虑的事项吗?


????类文章
????field:title,type:String

????类文章
????field:title,type:String,localize:true

我对我的文章模型有内容,并希望随后添加本地化.

现在我发现有时内容会显示为哈希

{"en"=>"asdfasdf","de"=>"123123123"}

有时作为普通文本.

如果我通过展示文章

@article.title
# I get --> no implicit conversion from nil to integer


@article.title_translations
# I get --> no implicit conversion from nil to integer

如果文档未翻译,则会发生这种情况. (串).

额外

此外,如果我试图改变内容我有问题:

article.title # "en"
article.title_translations # "Test Title"
article.update_attribute(:name,c.name_translations)
# raises NoMethodError: undefined method `merge!' for "Test Title":String
article.name = c.title_translations
# raises NoMethodError: undefined method `merge!' for "Test Title":String

>如何正确地将字符串字段更改为“String,:localize => true”?
>是否有要反映的脚本或其他内容?

解决方法

如果文档未翻译并且您尝试访问此字段,则会引发错误:

no implicit conversion from nil to integer

如果您尝试更改它会引发错误:

raises NoMethodError: undefined method `merge!' for "Test Title":String

这似乎是一个mongoid bug.现在,您可以在添加’localize:true’后执行此操作以进行迁移:

I18n.locale = :en
Article.all.each do |article|
  begin
    article.title
  rescue
    b = article.attributes['title']
    article.unset('title')
    article.title = b
    article.save
  end
end

它成了我的伎俩.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读