Faruk Hossain
Faruk Hossain

Reputation: 597

How to implement dynamic dropdown on rails admin?

Followings are my models

class Country
  has_many: cities
end

class City
  belongs_to :country
end

class Airport
  belongs_to :city
  belongs_to :country
  # I really need to have both city_id and country_id on airport
end

I am using Rails Admin. When I go to add or edit an Airport I want to be able to dynamically generate city dropdown based of the selected country.

Is there any way I can achieve this?

Upvotes: 1

Views: 2188

Answers (2)

Wilianto Indrawan
Wilianto Indrawan

Reputation: 2404

Unfortunately, Rails Admin doesn't support it. You need to do it by yourself with your own script. Reference.

But fortunately, there is always a good guy in the world. Here is some reference you can learn. http://railscasts.com/episodes/88-dynamic-select-menus-revised

Upvotes: 1

Anand
Anand

Reputation: 6531

class Airport
  belongs_to :city
  belongs_to :country

  #customization of rails admin
  RailsAdmin.config do |config|
    config.model 'Airport' do
      edit do
        field :country_id, :enum do
          enum do
            Country.all.collect{|c| [c.name, c.id]}
          end
        end
        field :city_id, :enum do
          enum do
            City.all.collect{|c| [c.name, c.id]}
          end
        end
      end
    end
  end
end

Upvotes: 0

Related Questions