I am working on a rails application that I building Active Admin over to manipulate the data. I have the following models for a car dealership:
class Make < ActiveRecord::Base
has_many :models
end
class Model < ActiveRecord::Base
belongs_to :make
has_many :cars
end
class Car < ActiveRecord::Base
belongs_to :model
def make
self.model.make
end
end
I am trying to adjust the form in the ActiveAdmin "Car" model, so that instead of choosing the model from a big list of all available models, I should be able to narrow down the model by selecting the "make" and then selecting the model based on that make.
currently, I have this:
ActiveAdmin.register Car do
controller do
def permitted_params
params.permit!
end
end
form :html => { :multipart => true } do |f|
f.inputs "Project Details" do |c|
f.input :vin
f.input :year
end
f.inputs "Make and model" do
f.input :maker, :as => :select, :collection => Make.all, :include_blank => false
f.input :model, :as => :select, :collection => Model.where(make_id: 1), :include_blank => false, :selected => (car.model.id if !car.model.nil?)
end
end
end
This works fine, and the models listed in the "Model" dropdown list are the ones that have a make_id = 1, as I coded. I would like the models to reflect the selected "maker". and a probably a button that updates the models list based on the make.
How can I do this?