首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Globalize3设置所有相同的语言

Globalize3设置所有相同的语言
EN

Stack Overflow用户
提问于 2013-06-21 04:17:59
回答 1查看 106关注 0票数 0

我有一个表单,我有3个编辑(每种语言一个)。当我检查之后,文本没有被插入到DB ok中。(我得到的是相同文本的3倍)。

这是在ingredient_category的控制器中,在这里,它在每次插入或更新之后调用update_other_locals_for,这被称为or。

代码语言:javascript
复制
after_filter lambda { |controller| controller.update_other_locals_for(@ingredient_category) }, :only => [:create, :update]

这是被调用的ApplicationController中的代码

代码语言:javascript
复制
  available_locals.each do |available_locale|
    I18n.locale = available_locale
    params_object = "#{available_locale}_" + item.class.to_s.underscore.downcase
    if params[params_object.to_sym].present?
      item.update_attributes(params[params_object.to_sym])
    end
  end

update行正在命中,正确的文本正在到达这一点,但当我在调试时,我在查询中看到了以下内容:

代码语言:javascript
复制
Started PUT "/ingredient_categories/4" for 127.0.0.1 at 2013-06-20 21:58:34 +0200
Processing by IngredientCategoriesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"aZFuv8v19oZcBkDmUzRzNyMUhBXnL5X0WvSyxNZhEuQ=", "ingredient_category"=>{"name"=>"Dutch"}, "en_ingredient_category"=>{"name"=>"English"}, "fr_ingredient_category"=>{"name"=>"French"}, "id"=>"4"}
  Shop Load (0.8ms)  SELECT "shops".* FROM "shops" WHERE "shops"."subdomain" = '' LIMIT 1
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 27 LIMIT 1
  IngredientCategory Load (0.5ms)  SELECT "ingredient_categories".* FROM "ingredient_categories" WHERE "ingredient_categories"."id" = ? LIMIT 1  [["id", "4"]]
  CACHE (0.1ms)  SELECT "ingredient_categories".* FROM "ingredient_categories" WHERE "ingredient_categories"."id" = ? LIMIT 1  [["id", "4"]]
   (0.3ms)  begin transaction
  IngredientCategory::Translation Load (0.6ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4
   (0.5ms)  UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 19:58:34.880306' WHERE "ingredient_categories"."id" = 4
  SQL (3.2ms)  INSERT INTO "ingredient_category_translations" ("created_at", "ingredient_category_id", "locale", "name", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Thu, 20 Jun 2013 21:58:34 CEST +02:00], ["ingredient_category_id", 4], ["locale", "en"], ["name", nil], ["updated_at", Thu, 20 Jun 2013 21:58:34 CEST +02:00]]
  IngredientCategory::Translation Load (0.5ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
   (0.7ms)  UPDATE "ingredient_category_translations" SET "name" = 'Dutch', "updated_at" = '2013-06-20 19:58:34.918413' WHERE "ingredient_category_translations"."id" = 31
  IngredientCategory::Translation Load (0.4ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1  [["id", 31]]
   (2.2ms)  commit transaction
Redirected to http://127.0.0.1:3000/ingredient_categories
   (0.3ms)  begin transaction
   (0.6ms)  UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 20:00:23.273350' WHERE "ingredient_categories"."id" = 4
  IngredientCategory::Translation Load (0.4ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
   (0.4ms)  UPDATE "ingredient_category_translations" SET "name" = 'English', "updated_at" = '2013-06-20 20:00:23.294591' WHERE "ingredient_category_translations"."id" = 31
  IngredientCategory::Translation Load (0.4ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1  [["id", 31]]
   (3.2ms)  commit transaction
   (0.3ms)  begin transaction
   (0.5ms)  UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 20:01:19.871801' WHERE "ingredient_categories"."id" = 4
  IngredientCategory::Translation Load (0.6ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
   (0.4ms)  UPDATE "ingredient_category_translations" SET "name" = 'French', "updated_at" = '2013-06-20 20:01:19.892798' WHERE "ingredient_category_translations"."id" = 31
  IngredientCategory::Translation Load (0.4ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1  [["id", 31]]
   (1.1ms)  commit transaction
Completed 302 Found in 187809ms (ActiveRecord: 19.2ms)

我注意到这一点:

代码语言:javascript
复制
AND "ingredient_category_translations"."locale" = 'en'

所有的更新。为什么不设置相应的语言呢?

EN

回答 1

Stack Overflow用户

发布于 2013-06-21 05:41:56

通过更改I18n.locale来进行更新似乎不是一个好主意。如果你用Globalize.with_locale(available_locale)来做这件事,它就像是一个魔咒!

代码语言:javascript
复制
  available_locals.each do |available_locale|
    Globalize.with_locale(available_locale) do
      params_object = "#{available_locale}_" + item.class.to_s.underscore.downcase
      if params[params_object.to_sym].present?
        item.update_attributes(params[params_object.to_sym])
      end
    end
  end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17223006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档