Ruby on rails 3 轨道强制所有平移

Ruby on rails 3 轨道强制所有平移,ruby-on-rails-3,models,rails-admin,globalize3,Ruby On Rails 3,Models,Rails Admin,Globalize3,感谢您,我正在将globalize3与rails\u admin一起使用。让我不舒服的是,用户可以添加任意多的翻译 此外,他不必在每一个语言环境中翻译内容(如I18n.available\u locales)。我喜欢。你如何处理这种情况 型号(简称): class项目:destroy,:inverse\u of=>:project 接受以下内容的\u嵌套\u属性\u:project\u translations,:allow\u destroy=>true 类ProjectTranslation

感谢您,我正在将
globalize3
rails\u admin
一起使用。让我不舒服的是,用户可以添加任意多的翻译

此外,他不必在每一个语言环境中翻译内容(如
I18n.available\u locales
)。我喜欢。你如何处理这种情况

型号(简称):

class项目:destroy,:inverse\u of=>:project
接受以下内容的\u嵌套\u属性\u:project\u translations,:allow\u destroy=>true
类ProjectTranslation
我最终改用了plus。更简单。

它也给我带来了麻烦,所以我创建了不允许它的自定义字段类型

主要类别:

module RailsAdmin
  module Config
    module Fields
      module Types
        class GlobalizeTabs < RailsAdmin::Config::Fields::Association
          RailsAdmin::Config::Fields::Types::register(:globalize_tabs, self)

          register_instance_option :partial do
            :form_globalize_tabs
          end

          def method_name
            "#{super}_attributes".to_sym
          end

          # Reader for validation errors of the bound object
          def errors
            bindings[:object].errors[name]
          end

          def available_locales
            I18n.available_locales
          end
          def current_locale
            I18n.locale
          end

          # Returns array of Translation objects
          # It gets existing or creates new empty translation for every locale
          # It's used in fields_for method in partial
          def translations
            translated_locales = @bindings[:object].translated_locales
            available_locales.collect do |locale|
              translated_locales.include?(locale) ? @bindings[:object].translation_for(locale) : @bindings[:object].translations.new({ locale: locale })
            end
          end
        end
      end
    end
  end
end
它使用自定义字段类中的
field.translations
方法,该方法返回一个翻译对象数组。 每个翻译对象都对应于可用的语言环境,它要么是数据库中的现有对象(如果已经存在翻译),要么是新的空翻译对象

例如

您有以下可用的区域设置:

I18n.available\u locales=[:en,:cz,:ru]

您有一个页面模型,其中包含一些已翻译的字段

此外,还有一个类页面的对象(数据库中的一行),该对象具有:en和:cz语言环境的翻译,但缺少:ru的翻译

因此,
\u form\u globalize\u tabs中的
field.translations
方法返回一个数组,该数组包含: 2个现有翻译:en和:cz,1个刚刚初始化翻译:ru

在部分中,我将此数组从
nested\u form
gem传递给
fields\u的
helper方法,该方法为每个翻译对象返回3个字段集


如果您不想自己弄乱代码,您可以使用此gem:

您好,请不要误解我的意思,但是不要只使用链接回答问题,请给出更详细的解释或在此处发布一些代码,并将链接作为源代码提供。对不起,我认为该链接就足够了。(如果你关注链接,这个gem的工作原理并没有那么糟糕的描述)无论如何,如果你愿意,我会更新答案。
module RailsAdmin
  module Config
    module Fields
      module Types
        class GlobalizeTabs < RailsAdmin::Config::Fields::Association
          RailsAdmin::Config::Fields::Types::register(:globalize_tabs, self)

          register_instance_option :partial do
            :form_globalize_tabs
          end

          def method_name
            "#{super}_attributes".to_sym
          end

          # Reader for validation errors of the bound object
          def errors
            bindings[:object].errors[name]
          end

          def available_locales
            I18n.available_locales
          end
          def current_locale
            I18n.locale
          end

          # Returns array of Translation objects
          # It gets existing or creates new empty translation for every locale
          # It's used in fields_for method in partial
          def translations
            translated_locales = @bindings[:object].translated_locales
            available_locales.collect do |locale|
              translated_locales.include?(locale) ? @bindings[:object].translation_for(locale) : @bindings[:object].translations.new({ locale: locale })
            end
          end
        end
      end
    end
  end
end
.controls
  = form.errors_for(field)
  %ul.nav.nav-tabs{ :style => 'margin-top:5px' }
    - field.available_locales.each do |locale|
      %li{ class: ( 'active' if locale == field.current_locale ) }
        %a{ href: "##{locale}", data: { toggle: "tab" } }= locale
.tab-content
  = form.fields_for field.name, field.translations, wrapper: false do |nested_form|
    .fields.tab-pane{ id: nested_form.object.locale, class: ( 'active' if nested_form.object.locale == field.current_locale ) }
      = nested_form.generate({:action => :nested, :model_config => field.associated_model_config, :nested_in => field.name })
= form.help_for(field)