一个取决于ActiveAdmin形式的其他输入字段的输入

发布于 2025-02-12 19:47:35 字数 381 浏览 0 评论 0 原文

假设我有一个模型:

class User
 has_many :books
end

class Book
 belongs_to :user
end

现在在Active Admin中,我需要选择任何用户时。该表格将仅显示由该用户创建的书籍。

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 的查询?

Let's say I have a model:

class User
 has_many :books
end

class Book
 belongs_to :user
end

Now in active admin, I want when I select any user. The form will only display books created by that user.

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      

What is the query to replace Book.all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2025-02-19 19:47:35

我对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

主动admin表单上的依赖性“选择”的这项工作

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 costorsController&lt; ApplicationController
    
      def sub_sectors_filter
        如果参数[:sector_id]
          sector = sector.find(params [:sector_id])
          @sub_sectors = sector.sub_sectors
        别的
          @sub_sectors = []
        结尾
        渲染:json =&gt; @sub_sectors.collect {| sub_sector | {:id =&gt; sub_sector.id,:name =&gt; sub_sector.name}}
       结尾
    
    结尾
     
  2. doutes.rb 文件中添加路由。

     获取'sub_sectors_filter/'=&gt; 'sub_sectors#sub_sectors_filter'
     
  3. 使用您的Web浏览器控制您的选择您的选择,并使用CSS选择器为Select创建jQuery对象,例如:

      $('#andurand_sector_id')
     
  4. 在文件/app/app/asps/javascripts/active_admin中添加此代码块。 JS ,负责调用生成的控制器,并添加其返回的选项。

      // =需要Active_admin/base
    $(function(){
      美元
        $('#andurans_sub_sector_id选项')。remove(); //删除All&lt;选项&gt;子标签。
        $ .getJSON(`/sub_sectors_filter`,{sector_id:$($(this).val()},function(result){// getjson上的文档:http://api.jquery.com/jquery.com/jquery.com/jquery.getjson/
          $。each(结果,函数(i,item){//通过集合进行迭代
            美元
                价值:item.id,
                文字:item.Name 
            }));
          });
        });
      }))
    }))
     

参考文献:

无法在我的依赖选择下拉admin上找到错误(Rails 3.2,Active Admin 1.0)

This work for me on dependend "select" on Rails 6.0.4

Models

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

Active admin form

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

You must do the following:

  1. Create a controller, that returns the subsectors.

    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. Add the route in the routes.rb file.

    get 'sub_sectors_filter/' => 'sub_sectors#sub_sectors_filter'
    
  3. Inspect with your web browser console your selects, and use a CSS selector to create a jQuery object for the sector select, something like:

    $('#organization_sector_id')
    
  4. Add this block of code in the file /app/assets/javascripts/active_admin.js, which is responsible for making the call to the generated controller, and adding the options that it returns.

    //= 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 
            }));
          });
        });
      })
    })
    

References:

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

浅浅 2025-02-19 19:47:35

AS sampat 在评论中提到您可以使用activeadmin 嵌套选择插件

安装 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


As Sampat mentioned in the comment you can use ActiveAdmin nested select addon.

After installing activeadmin_addons gem, you can use:

  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


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文