【问题标题】:What is the proper way to integrate enum with administrate?将枚举与管理集成的正确方法是什么?
【发布时间】:2020-07-27 17:58:49
【问题描述】:

我有一个 Rails 应用程序,其中用户有一个性别,这是一个枚举,其中 0 表示女性,1 表示男性。

我在 user_dashboard.rb 中有这段代码:

require "administrate/base_dashboard"

class UserDashboard < Administrate::BaseDashboard
    ATTRIBUTE_TYPES = {
        posts: Field::HasMany,
        id: Field::Number.with_options(searchable: false),
        email: Field::String.with_options(searchable: true),
        password: Field::String.with_options(searchable: false),
        password_confirmation: Field::String.with_options(searchable: false),
        encrypted_password: Field::String.with_options(searchable: false),
        reset_password_token: Field::String.with_options(searchable: false),
        reset_password_sent_at: Field::DateTime.with_options(searchable: false),
        remember_created_at: Field::DateTime.with_options(searchable: false),
        first_name: Field::String.with_options(searchable: false),
        last_name: Field::String.with_options(searchable: false),
        gender: Field::Text.with_options(searchable: false),
        type: Field::String.with_options(searchable: false),
        created_at: Field::DateTime.with_options(searchable: false),
        updated_at: Field::DateTime.with_options(searchable: false),
        phone: Field::String.with_options(searchable: false),
    }.freeze

    COLLECTION_ATTRIBUTES = %i[
        posts
        email
        phone
        type
    ].freeze

    SHOW_PAGE_ATTRIBUTES = %i[
        posts
        id
        email
        phone
        first_name
        last_name
        gender
        type
        created_at
        updated_at
    ].freeze

    FORM_ATTRIBUTES = %i[
        posts
        email
        phone
        first_name
        last_name
        gender
        password
        password_confirmation
        type
    ].freeze

    COLLECTION_FILTERS = {}.freeze
end

new_admin_user_path 的视图是:

只有管理员可以创建用户,但他们必须手动输入“男性”或“女性”等性别。有没有办法集成一个可以与管理 gem 一起使用的选择菜单或单选按钮?

【问题讨论】:

    标签: ruby-on-rails administrate rails-administrate


    【解决方案1】:

    一种选择是在您的 ATTRIBUTE_TYPES 中执行此操作:

    ATTRIBUTE_TYPES = {
      ...
      gender: Field::Select.with_options(collection: ["female", "male"]),
    }
    

    您也可以尝试使用 AdministrateFieldEnum gem。 https://github.com/valiot/administrate-field-enum

    【讨论】:

      【解决方案2】:

      假设 genderapp/person.rb 模型的枚举字段:

      class Person < ApplicationRecord
        enum gender: { female: 0, male: 1 }
        # . . .
      end
      

      您的 20200922125209_create_persons.rb 迁移在哪里:

      class CreatePersons < ActiveRecord::Migration[6.0]
        def change
          create_table :persons do |t|
            t.integer :gender
      # . . .
      

      在您的 app/dashboards/person_dashboard.rb 中添加以下内容:

        ATTRIBUTE_TYPES = {
          # . . .
          gender: Field::Select.with_options(searchable: false, collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys }),
          # . . .
        }
      

      然后,只需将 gender 字段添加到您的 COLLECTION_ATTRIBUTES、SHOW_PAGE_ATTRIBUTES 数组中。

      Administrate 的魔力将处理其余的事情,在显示和索引视图中显示“女性”或“男性”,并在编辑表单中显示选择下拉菜单。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-05
        • 2014-04-10
        相关资源
        最近更新 更多