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

问题描述 投票:0回答:1

我正在使用带有Active Admin的Ruby on Rails 5 API应用程序。我有一个名为Subscription with has_many courses的模型(课程也是一个模型)。

当我向订阅添加课程时,我需要逐个完成,如下图所示:enter image description here

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

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
ruby-on-rails activeadmin has-many formtastic multiple-select
1个回答
1
投票

对于数组使用as: :select表单输入多选应该有帮助。像这样的东西:

sub_c.input :items, as: :select, 
                    collection: courses_collection, 
                    multiple: true 
© www.soinside.com 2019 - 2024. All rights reserved.