rails_admin无法使用associated_model_config修改嵌套模型的字段

时间:2015-07-09 12:58:41

标签: ruby-on-rails mongoid rails-admin

我正在尝试在:has_many关联中配置嵌套模型的字段,但没有发生任何事情。我在Rails 4.2.3mongoid 4.0.2rails_admin 0.6.8

Property有许多Characteristic s,Product拥有并属于许多Characteristic s:

/app/models/property.rb

class Property
  include Mongoid::Document
  include Mongoid::Timestamps

  field :handle
  field :title, localize: true

  has_many :characteristics, inverse_of: :property
  accepts_nested_attributes_for :characteristics, allow_destroy: true

  validates :handle, presence: true
  validates :title, presence: true
end

/app/models/characteristic.rb

class Characteristic
  include Mongoid::Document
  include Mongoid::Timestamps

  field :handle
  field :title, localize: true

  belongs_to :property, inverse_of: :characteristics

  has_and_belongs_to_many :products

  validates :property, presence: true
  validates :handle, presence: true
  validates :title, presence: true
end

/app/models/product.rb

class Product
  include Mongoid::Document
  include Mongoid::Timestamps

  field :handle
  field :title, localize: true
  field :page_title, localize: true
  field :description, localize: true
  field :short_description, localize: true
  field :meta_description, localize: true
  field :meta_keywords, localize: true

  has_and_belongs_to_many :characteristics

  validates :handle, presence: true
  validates :title, presence: true
end

/config/initializers/rails_admin.rb

require 'i18n'

I18n.available_locales = [:ru, :en]
I18n.default_locale = :ru

RailsAdmin.config do |config|
  config.main_app_name = Proc.new { |controller| [ "Babylon", "BackOffice - #{controller.params[:action].try(:titleize)}" ] }

  config.actions do
    dashboard
    index
    nestable
    new
    export
    bulk_delete
    # show
    edit
    delete
    show_in_app

    ## With an audit adapter, you can add:
    # history_index
    # history_show
  end

  config.model Characteristic do
    visible false
    label I18n.t(:characteristic).capitalize
    label_plural I18n.t(:characteristics).capitalize

    object_label_method do
      :i18n_characteristic
    end

    create do
      field :title do
        label I18n.t(:title)
      end
    end

    update do
      field :title do
        label I18n.t(:title)
      end
    end
  end

  config.model Property do
    visible true
    label I18n.t(:property).capitalize
    label_plural I18n.t(:properties).capitalize

    object_label_method do
      :i18n_property
    end

    list do
      field :title do
        label I18n.t(:title).capitalize

        formatted_value do
          bindings[:view].link_to value[I18n.locale], bindings[:view].rails_admin.edit_path(model_name: 'property', id: bindings[:object]._id.to_s)
        end 
      end
      field :handle do
        label I18n.t(:handle).capitalize
      end
    end

    create do
      field :title do
        label I18n.t(:title)
      end

      group I18n.t(:seo).upcase! do
        field :handle do
          label I18n.t(:handle)
        end
      end

      group I18n.t(:characteristics).capitalize do
        field :characteristics do
          label I18n.t(:characteristics)

          associated_model_config do
            field :title do
              label  I18n.t(:title).capitalize
            end
          end
        end
      end
    end

    update do
      field :title do
        label I18n.t(:title)
      end

      group I18n.t(:seo).upcase! do
        field :handle do
          label I18n.t(:handle)
        end
      end

      group I18n.t(:characteristics).capitalize do
        field :characteristics do
          label I18n.t(:characteristics)

          associated_model_config do

            field :title do
              label  I18n.t(:title).capitalize
            end
          end
        end
      end
    end
  end

  ...
end

def i18n_property
  title_translations[I18n.locale]
end
def i18n_characteristic
  title_translations[I18n.locale]
end

如您所见,我尝试在2个地方配置Characteristic模型的字段以仅显示:title字段,但它仍会自动显示它可以从模型中获取的所有内容,而不是甚至翻译:

enter image description here

请帮助找到正确显示嵌套字段的解决方案!谢谢。

1 个答案:

答案 0 :(得分:0)

好的,这都是因为订单错误。要使所有工作正常,订单应首先Property,然后才Characteristic。我希望在官方自述文件中提到它。