简体   繁体   中英

Rails:how to show name instead of ID or Address in has_many relation in active_admin?

These days I'm using the active_admin to manage my data. I have a Audio model and Problem model. Audio has many problems and Problem belongs to audio. I use the active_admin to create the problems. But in the problem's new page, there is a drop-down list shows the content like:

#<Audio:0xb4116084>

With the address I can hardly recognize which file I want. What I want to show in the Audio's drop-down list is the Audio's title which is a column of Audio model. I just want to change this column in the new page, and others remain the same as default. What should I do? Thanks!

Audio class must implement display_name method

Ex

class Audio
   def display_name
     title
   end
end

this is from active admin sources

 # Active Admin makes educated guesses when displaying objects, this is
    # the list of methods it tries calling in order
    setting :display_name_methods, [ :display_name,
                                      :full_name,
                                      :name,
                                      :username,
                                      :login,
                                      :title,
                                      :email,
                                      :to_s ]

Looks like you haven't such methods so to_s is called for Audio objects

You can use a :member_label.

Here is a example

form :html => { :enctype => "multipart/form-data" }  do |f| 
  f.input :problems,
          :input_html => { :multiple => false, :style => "width: 700px;"},
          :collection => Audio.all,
          :member_label => :audio_name
end

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