Ruby Rails 和 ActiveAdmin:一次添加所有对象,而不是一个一个添加

Ruby on Rails and ActiveAdmin: add all object all at once instead add one by one

我在 Rails 5 API 应用上使用 Ruby 和 Active Admin。 我有一个名为 Subscription 的模型,包含 has_many 门课程(课程也是一个模型)。

当我将课程添加到订阅中时,我需要一个接一个地添加,如下图所示:

我想要的是可以选择一次性将所有课程添加到订阅中,而不是一个一个地添加。我尝试使用复选框进行多项选择,但没有用。这是代码:

ActiveAdmin.register Subscription do
  # require 'lib/app_languages.rb'
  permit_params :name, :seat_limit, :domain, :language, :organization, course_ids: [], user_ids: [], subscription_courses_attributes: [:id, :course_id, :_destroy, :_create, :_update]

  config.sort_order = 'id_asc'

  # Index
  index do
    id_column
    column :name
    column :seat_limit
    column :domain
    column :organization
    column :language do |subscription|
       AppLanguages.languages[subscription.language]
    end
    column "Unlocked courses", :courses
    column :created_at
    actions
  end

  # Show
  show do
    attributes_table do
     row  :name
     row  :language do |subscription|
        AppLanguages.languages[subscription.language]
     end
     row  :seat_limit
     row  :domain
     row  :organization
     row  :created_at
    end

    panel 'Unlocked Courses' do
      table_for subscription.courses do
        column :id
        column "Title" do |course|
          link_to course.title, admin_course_path(course)
        end
        column "language" do |course|
          AppLanguages.languages[course.language]
        end
        column :author
      end
    end

    panel 'Subscribed users' do
      table_for subscription.users do
        column :id
        column "name" do |user|
          link_to user.name, admin_user_path(user)
        end
        column :email
        column "language preference" do |user|
          AppLanguages.languages[user.language]
        end
      end
    end
  end

  # Edit
  form do |f|
    f.semantic_errors *f.object.errors.keys

    f.inputs 'Details' do
     f.input :name
     f.input :language, :as => :select, :collection => AppLanguages.languages_array
     f.input :seat_limit
     f.input :domain
     f.input :organization
    end


    f.has_many :subscription_courses do |sub_c|
      sub_c.inputs "Unlocked Courses" do
        if !sub_c.object.nil?
          sub_c.input :_destroy, as: :boolean, label: "Destroy?"
        end
        sub_c.input :course ### should add here option for add all
      end
    end

    f.inputs 'Subscribed users:' do
      f.input :users
    end

    f.actions
  end
end

as: :select 表单输入与 multi-select 用于数组应该有所帮助。像这样:

sub_c.input :items, as: :select, 
                    collection: courses_collection, 
                    multiple: true 

您可以尝试使用 check_boxes

sub_c.input :items, as: :check_boxes, collection: courses_collection