简体   繁体   中英

How to create activeadmin dropdown menu form input that contains all foreign key attribute values

what is the code I have to write inside one of my activeadmin controllers to show a dropdown menu form input for one of my model attributes ?

knowing that this attribute is a foreign key for a primary key in another table, I want it to appear like the category attribute appears in this photo.

在此处输入图片说明

so this is the relation between my two tables, Item & Photo, where Item table has_many Photos, which is many to one.

在此处输入图片说明

I edited the code inside my admin/photos.rb to be like that

f.input :item_id, label: 'Item', 
      as: :select, collection: -> { Photo.pluck(:item_id, :id) }

where the item_id is the foreign key in Photo model, and the :id is the primary key in Item model, but all I got was that error

在此处输入图片说明

so any solutions ?

Are you looking for something like this?

  f.input :item_id, label: 'Item', 
          as: :select, collection: -> { Item.pluck(:name, :id) }

In this case :code is the attribute displayed in the dropdown and :id is the primary key matching the :carrier_id foreign key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM