Ruby On Rails Active Admin has_many changes the drop-down list to use a different column

I am completely new to ActiveAdmin and RoR, and I cannot figure out how to change the visible value of the drop-down lists in the has_many association.

Fillup Model

class Fillup < ActiveRecord::Base // key is car_id:integer belongs_to :car end 

Car model

 class Car < ActiveRecord::Base validates :description, :presence => true key is fillup_id:integer has_many :fillups end 

What is he showing now:

He currently shows im, suggesting an encoded link to the Car assigned to him. enter image description here

What I need to show:

I need this to show the description that is defined as description:string in the Car Model . enter image description here

+4
source share
2 answers

Something like this should work ...

In app/admin/model_name.rb

 form do |f| f.inputs "My Model Name" do # add your other inputs f.input :cars, :collection => Car.all.map{ |car| [car.description, car.id] } f.buttons end end 

Read this article to learn more about changing forms.

AciveAdmin uses formtastic , you should also read about it.

+10
source

In your Car model, just add something like:

 def to_s description end 

He must do the job!

Explanation: In fact, your Car to_s method returns the object identifier corresponding to the current instance, which is used by default when using the method, for example, puts on the object. To replace the model display name, you must override this method and it will work anywhere in your application when you use puts @car or in your templates that execute <%= @car %>

+5
source

Source: https://habr.com/ru/post/1387228/


All Articles