【问题标题】:One input dependent on other input field in activeadmin form一个输入依赖于 activeadmin 表单中的其他输入字段
【发布时间】:2022-08-05 10:02:09
【问题描述】:

假设我有一个模型:

class User
 has_many :books
end

class Book
 belongs_to :user
end

现在在活动管理员中,当我选择任何用户时我想要。该表单将仅显示该用户创建的书籍。

forms do |f|
 f.inputs do
  f.input :user, as: :select, collection: User.all
  f.input :books, as: :select,  collection: Book.all
 end
 f.actions
end      

替换Book.all 的查询是什么?

  • 我认为你需要javascript来解决这个问题。因为在页面加载并且您从选择框中选择用户之前,不会决定选择哪个用户。
  • ActiveAdmin Addons gem 提供在 ActiveAdmin 中集成 nested select 的选项

标签: ruby-on-rails ruby forms activeadmin


【解决方案1】:

正如评论中提到的Sampat,您可以使用ActiveAdmin nested select addon

安装activeadmin_addons gem 后,可以使用:

  forms do |f|
    f.inputs do
      f.input :user_id, as: :nested_select,
                        level_1: { attribute: :user_id, collection: User.all },
                        level_2: { attribute: :book_id, collection: Book.all }
    end
    f.actions
  end


【讨论】:

    【解决方案2】:

    这项工作对我来说取决于 Rails 6.0.4 上的依赖“选择”

    楷模

    class Organization < ApplicationRecord 
     belongs_to :sector
     belongs_to :sub_sectors
    end
    
    class Sector < ApplicationRecord 
     has_many :sub_sectors
    end
    
    class SubSector < ApplicationRecord 
     belongs_to :sector
    end
    

    活动管理表单

    ActiveAdmin.register Organization do
      form do |f|
        f.inputs "Details" do
            f.input :sector, as: :select, collection: Sector.all
            f.input :sub_sector_id, as: :select, collection: ([]) # The input is initialized without values
        end
        f.actions
      end
    end
    

    您必须执行以下操作:

    1. 创建一个控制器,它返回子扇区。

      class SubSectorsController < ApplicationController
      
        def sub_sectors_filter
          if params[:sector_id]
            sector = Sector.find(params[:sector_id])
            @sub_sectors = sector.sub_sectors
          else
            @sub_sectors = []
          end
          render :json => @sub_sectors.collect {|sub_sector| {:id => sub_sector.id, :name => sub_sector.name} }
         end
      
      end
      
    2. routes.rb 文件中添加路由。

      get 'sub_sectors_filter/' => 'sub_sectors#sub_sectors_filter'
      
    3. 使用您的 Web 浏览器控制台检查您的选择,并使用 CSS 选择器为扇区选择创建一个 jQuery 对象,例如:

      $('#organization_sector_id')
      
    4. 在文件/app/assets/javascripts/active_admin.js 中添加这段代码,该文件负责调用生成的控制器,并添加它返回的选项。

      //= require active_admin/base
      $(function () {
        $('#organization_sector_id').on('change', function () {
          $('#organization_sub_sector_id option').remove(); // Remove all <option> child tags.
          $.getJSON(`/sub_sectors_filter`, {sector_id: $(this).val()}, function(result){ // Documentation on getJSON: http://api.jquery.com/jQuery.getJSON/
            $.each(result, function (i, item) { // Iterates through a collection
              $('#organization_sub_sector_id').append($('<option>', { // Append an object to the inside of the select box
                  value: item.id,
                  text : item.name 
              }));
            });
          });
        })
      })
      

      参考:

      Can't find the error in my dependent select drop down on Active Admin( Rails 3.2, Active Admin 1.0)

      Populate Select box options on click with Javascript/Jquery with Json data

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2019-01-19
      相关资源
      最近更新 更多