0

我有一个表格,其中有 3 个编辑(每种语言一个)。当我事后检查时,文本没有插入数据库中。(我得到相同文本的 3 倍)。

这是在成分类别的控制器中,这里它在每次插入或更新后调用update_other_locals_for,这被调用了。

after_filter lambda { |controller| controller.update_other_locals_for(@ingredient_category) }, :only => [:create, :update]

这是 ApplicationController 中被调用的代码

  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

更新行被击中并且正确的文本到达那个点但是当我调试时我在查询中看到这个:

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)

我注意到了这一点:

AND "ingredient_category_translations"."locale" = 'en'

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

4

1 回答 1

0

通过更改 I18n.locale 来进行更新似乎是个坏主意。如果你用 Globalize.with_locale(available_locale) 来做,它就像一个魅力!

  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
于 2013-06-20T21:41:55.560 回答