Using rails_admin to display dropdown list on belongs_to association
Asked Answered
I

1

6

I am using rails_admin to manage data in a rails application.

I have this class:

class Activity < ActiveRecord::Base
  attr_accessible :content, :title, :category_id
  belongs_to :category, :inverse_of => :activities
end

And the other end is:

class Category < ActiveRecord::Base
  attr_accessible :title, :category_id, :activities_ids
  has_many :activities, :inverse_of => :category
end

My rails_admin initialiser for Activity looks like this:

config.model Activity do
  edit do
    field :title
    field :content, :text do
      bootstrap_wysihtml5 true
    end
    field :category
  end
end

Now, in the form it renders the category like this:

Missing field

It is supposed to render the names of categories, right? What am I missing here?

Interferometer answered 1/3, 2013 at 15:58 Comment(1)
It looks like it should work, are there definitely categories currently defined, i.e. not blank?Airdry
E
5

I've been looking for this for quite a while. To get a really nice autocomplete dropdown just add:

config.model Activity do
  edit do
    field :category, :belongs_to_association
  end
end
Eaves answered 13/6, 2013 at 11:58 Comment(1)
I tried this with a similar belongs_to association and the associated records still show as Station #1 Station #2. Any thoughts on why? I want the actual station names to display instead of the objects.Intractable

© 2022 - 2024 — McMap. All rights reserved.