【问题标题】:How can I customize only a field in rails_admin?如何仅自定义 rails_admin 中的字段?
【发布时间】:2011-04-02 16:35:58
【问题描述】:

我有一个字段,rails_admin 为其生成一个文本字段,但我希望它改用 <select> 标记。我在初始化程序中自定义了这样的字段:

RailsAdmin.config do |config|
  config.model User do
    update do
      field :state do
        partial "user_state_partial"
      end
    end
  end
end

我已经测试过了,它可以工作。问题是,通过这样做(我也尝试使用编辑块),剩下的唯一字段是我正在自定义的字段。有没有办法告诉 rails_admin 只假设其他字段的默认值?

【问题讨论】:

    标签: rails-admin


    【解决方案1】:

    更好(更短)的解决方案是使用“配置”语法而不是“字段”。 通过使用 configure,rails_admin 将为所有其他值使用默认值。

    例如,使用以下代码:

    RailsAdmin.config do |config|
      config.model User do
        update do
          configure :state do
            partial "user_state_partial"
          end
        end
      end
    end
    

    ...将允许 RailsAdmin 对 :state 使用指定的部分,但它将对所有其他字段使用默认值。

    更多信息请访问:Rails Admin wiki

    【讨论】:

      【解决方案2】:

      一旦定义了一个字段,就必须定义所有要使用的字段。 默认为所有字段。

      RailsAdmin.config do |config|
        config.model User do
          update do
            field :name
            field :surname
            field :state do
              partial "user_state_partial"
            end
          end
        end
      end
      

      【讨论】:

        【解决方案3】:

        当前的文档说你可以,像这样:

        field :name do
          # snipped specific configuration for name attribute
        end
        
        include_all_fields # all other default fields will be added after, conveniently
        exclude_fields :created_at # but you still can remove fields
        

        ...但它仍然会删除关联子表单。 (您可以使用“field :association_id”(不是“field :association”)添加belongs_to 项目,但我不确定如何添加has_* 子表单。

        【讨论】:

          【解决方案4】:

          我通常做 include_all_fields,然后为我的字段自定义配置,然后添加 exclude_fields(用于 id 和时间戳等字段)。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多