1

I'm using ActiveAdmin for an admin panel. In the nested form, I need to use some filtering, for example, in the dropdown I need to select an option name, and in the second dropdown should be displayed the option's values.

For this, I want to use activeadmin-ajax_filter gem. Option and OptionValue are connected with each other: Option has_many :option_values, and OptionValue belongs_to Option. I create option_values through the nested form when I create the Option.

Then, I have a Product model, in which via nested form I create a Variant. So, Variant belongs_to :option_value belongs_to :product and OptionValue has_many :variants, Product has_many :variants.

Just for now, when I want to create a new variant, I can select all the option values I have in the db. I want to select an option (for example a size), and then, in a dropdown below - select the appropriate values (XL, S, and so on).

The code for my active_admin resources is:

ActiveAdmin.register Product do
    
  permit_params :category_id, :name, :description, :short_description, :image, :subtitle,
                product_currencies_attributes: [:id, :product_id, :currency_id, :price, :_destroy],
                variants_attributes: [:id, :product_id, :in_stock_id, :sold_out_id, :option_value_id, :image, :visible,
                                      :orderable, :_destroy]

  index do
    column :id
    column :image do |product|
      image_tag(product.image.url(:thumb)) if product.image
    end
    column :category_id do |product|
      category = Category.find(product.category_id)
      link_to category.name, admin_category_path(category)
    end
    column :name
    column :short_description
    column :description do |product|
      product.description.truncate(90)
    end
    column :subtitle
    column :created_at
    column :updated_at
    actions
  end

  show do
    tabs do
      tab 'Product' do
        attributes_table do
          row :id
          row :image do
            image_tag(product.image.url(:medium))
          end
          row :category_id do
            category = Category.find(product.category_id)
            link_to category.name, admin_category_path(category)
          end
          row :name
          row :short_description
          row :description
          row :subtitle
          row :created_at
          row :updated_at
        end
      end
      tab 'Prices' do
        attributes_table do
          row :prices do
            div do
              product.product_currencies.each do |product_currency|
                div do
                  "#{Currency.find(product_currency.currency_id).name}: #{product_currency.price}"
                end
              end
            end
          end
        end
      end
      tab 'Variants' do
        div do
          'Variants will be here soon =)'
        end
      end
    end

  end

  form do |f|
    tabs do
      tab 'Product' do
        f.inputs do
          f.input :category_id, as: :select, collection: Category.all.collect { |category| [category.name, category.id] }
          f.input :name
          f.input :short_description
          f.input :description
          f.input :subtitle
          f.input :image, as: :file
        end
      end
      tab 'Price' do
        f.inputs do
          f.has_many :product_currencies, allow_destroy: true, heading: false, new_record: 'Add New Price' do |product_currency|
            product_currency.template.render partial: 'product-price-form', locals: { product_currency: product_currency,
                                                                                      product_id: params[:id].to_i }
          end
        end
      end
      tab 'Variants' do
        f.inputs do
          f.has_many :variants, allow_destroy: true, heading: false, new_record: 'Add new Variant' do |variant|
            variant.template.render partial: 'variant-form', locals: { variant: variant, product_id: params[:id].to_i }
          end
        end
      end
      tab 'SEO' do
        div do
          'SEO inputs will be here'
        end
      end
    end
    f.actions
  end
       
end

So, I've installed activeadmin-ajax_filter gem. I tried to follow the documentation, but as a result - nothing. As far, as I understand, I have to put this line of code to my Option activeadmin model: activeadmin-ajax_filter, and in the form use this:

f.input :option, as: :ajax_select, data: { 
      url: filter_admin_options_path,
      search_fields: [:name], 
      static_ransack: { active_eq: true }, 
      ajax_search_fields: [:option_value_id],
    }

But still - nothing. By the way, the partial is:

<% if @product.new_record? %>
  <% if Product.any? %>
    <% new_product_id = Product.order(id: :desc).first.id + 1 %>
    <% variant.input :product_id, as: :hidden, input_html: { value: new_product_id } %>
  <% else %>
    <% variant.input :product_id, as: :hidden, input_html: { value: 1 } %>
  <% end %>
<% else %>
  <% variant.input :product_id, as: :hidden, input_html: { value: product_id } %>
<% end %>
<% variant.input :in_stock_id, as: :select, collection: InStock.all.collect { |in_stock| [in_stock.name, in_stock.id] } %>
<% variant.input :sold_out_id, as: :select, collection: SoldOut.all.collect { |sold_out| [sold_out.name, sold_out.id] } %>
<%# variant.input :option, as: :ajax_select, collection: Option.all.collect { |option| [option.name, option.id] },
                 # data: {
                 #         url: filter_admin_options_path,
                 #         serarch_fields: [:name],
                 #         static_ransack: { active_eq: true },
                 #         ajax_search_fields: [:option_value_id]
                 #       }
#%>
<% variant.input :option_value_id, as: :select, collection: OptionValue.all
    .collect { |option_value| [option_value.value, option_value.id] } %>
<% variant.input :visible %>
<% variant.input :orderable %>
<% variant.input :image, as: :file %>
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Alex Zakruzhetskyi
  • 1,383
  • 2
  • 22
  • 43

0 Answers0