6
votes

I use Active Admin gem for Ruby on Rails. I have modules Team and Coach, which have a has_many and belongs_to relationship.

class Team < ActiveRecord::Base
  belongs_to :coach
end

class Coach < ActiveRecord::Base
  has_many :teams
end

I figured out how to display first name and last name on index and show page (i did it like that:)

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

What i want is how to display first name and last name of coach in Team form (new and edit page) in dropdown menu? Please help me with this.

3

3 Answers

7
votes

Can you try this

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

I had the same problem. The edit page shows object instances in the select menu such as,

#<Coach:0x00eff180c85c8>

To solve it and access each instance's fields use this,

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 its documentation has more examples.

This stackoverflow answer helped me get this solution.

3
votes

Try this:

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