Do the ActiveAdmin forms (new / edit) belong_and associations?

I am using Active Admin gem for Ruby on Rails. I have Team and Coach modules that have has_many and belongs_to relationships.

class Team < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :teams end 

I figured out how to display the first and last name on the index page and the display page (I did it this way :)

  index do column :name column "Coach" do |team| team.coach.firstname + " " + team.coach.lastname end default_actions end 

What I want - how to display the name and surname of the trainer in the form of a team (new and right page) in the drop-down menu? Please help me with this.

+6
source share
3 answers

Can you try this

 f.input :coach_name, :as => :select, :collection => Coach.all.map {|u| [u.firstname, u.id]}, :include_blank => false 
+7
source

I had the same problem. The editing page displays instances of objects in the selection menu, for example,

 #<Coach:0x00eff180c85c8> 

To solve this problem and access these fields, use

 form do |f| f.inputs "Coaches" do f.input :name f.input :coach, member_label: Proc.new { |c| "#{c.firstname} #{c.lastname}" end f.actions end 

ActiveAdmin uses Formtastic, and the documentation contains more examples.

This stackoverflow answer helped me get this solution.

+3
source

Try the following:

 f.input :coach_name, :as => :select, :collection => Coach.all.map {|u| [u.firstname.to_s, u.id]} 
+3
source

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


All Articles