platanus / activeadmin_addons

Extends ActiveAdmin to enable a set of great optional UX improving add-ons
MIT License
752 stars 282 forks source link

[BUG] Nested Select not filtering based on selection #484

Open Andrew-Max opened 9 months ago

Andrew-Max commented 9 months ago

Describe the bug I've implemented a nested select and it all renders. But my the values in the second field are not scoped to the selection of the first field and if I look in the controller action that handles this, no ID is being passed to it at all to scope based off of

To Reproduce

class Order < ApplicationRecord
  belongs_to :shipping_address
  belongs_to :customer
class Customer < ApplicationRecord
  has_many :orders
  has_many :shipping_addresses
  accepts_nested_attributes_for :shipping_addresses
end
class ShippingAddress < ApplicationRecord
  has_many :orders
  belongs_to :customer
ActiveAdmin.register Order do
  form do |f|
    f.inputs "Customer" do
      f.input :nested_shipping_address,
              as: :nested_select,
              minimum_input_length: 0,
              level_1: {
                attribute: :customer,
              },
              level_2: {
                attribute: :shipping_address,
                display_name: :address_1,
              }
    end

When I bind in the controller that handles the index request, here are what the params look like: => #<ActionController::Parameters {"order"=>"name_desc", "q"=>{"groupings"=>{"0"=>{"m"=>"or", "name_cont"=>""}}, "combinator"=>"and", "customer_eq"=>"-1"}, "controller"=>"admin/shipping_addresses", "action"=>"index"} permitted: true>

Additional context I've also been completely unable to figure out how to view the js in the browser or to try to figure out a way to monkey patch any of this. Open to suggestions on that if anyone know what to do.

omar-mohamed commented 5 months ago

@Andrew-Max I had a similar issue where this code didn't filter:

      f.input :customer_car, as: :nested_select,
                             display_name: :display_name,
                             minimum_input_length: 0,
                             level_1: {
                               attribute: :customer,
                               minimum_input_length: 1,
                               fields: %i[id name phone]
                             },
                             level_2: {
                               attribute: :customer_car,
                               fields: %i[id plate_number]
                             }

It also sent "customer_eq"=>"-1". I made it work by making the attribute in level 1 customer_id

      f.input :customer_car, as: :nested_select,
                             display_name: :display_name,
                             minimum_input_length: 0,
                             level_1: {
                               attribute: :customer_id,
                               minimum_input_length: 1,
                               fields: %i[id name phone]
                             },
                             level_2: {
                               attribute: :customer_car,
                               fields: %i[id plate_number]
                             }

This made the parameters like this: {"order"=>"id_desc", "q"=>{"groupings"=>{"0"=>{"m"=>"or", "id_eq"=>"", "plate_number_cont"=>""}}, "combinator"=>"and", "customer_id_eq"=>"9"}, "customer_car"=>{}}

I'm on v2.0.0.beta.3