Ryan.lay
Ryan.lay

Reputation: 1761

ActiveAdmin & active_enum gem

I have the following values using the active_enum gem:

initializers/active_enum.rb

ActiveEnum.define do
  # defines Syllabus
  enum(:syllabus) do
    value :id => 1, :name => 'Trinity Rock & Pop'
    value :id => 2, :name => 'Trinity Guildhall'
    value :id => 3, :name => 'ABRSM'
  end
end

models/lesson.rb

class Lesson < ActiveRecord::Base
  enumerate :syllabus
end

admin/lessons.rb

ActiveAdmin.register Lesson do
  index do
    column :syllabus
  end
end

The ActiveAdmin index column shows the syllabus :id, how do I get it to show the syllabus :name?

I've tried

column :syllabus, :name
column :syllabus_name
column :syllabus.name

CRUD with AA works as intended with :names

Upvotes: 0

Views: 1168

Answers (2)

okliv
okliv

Reputation: 3959

according to AA Docs http://www.activeadmin.info/docs/3-index-pages/index-as-table.html (4th snippet) and ActiveEnum Documentation

you can get the name in this way:

column('Name') {|lesson| lesson.syllabus(:name)}

Upvotes: 2

Ryan.lay
Ryan.lay

Reputation: 1761

Solved. Might not be the best way to do the job but it's simple and it works:

initializers/active_enum.rb

ActiveEnum.define do
  # defines Syllabus
  enum(:syllabus) do
    value :id => 'Trinity Rock & Pop', :name => 'Trinity Rock & Pop'
    value :id => 'Trinity Guildhall', :name => 'Trinity Guildhall'
    value :id => 'ABRSM', :name => 'ABRSM'
  end
end

by changing the :id to match the :name, ActiveAdmin index columns displays the intended string.

Note:

  • using

initializers/active_enum.rb

config.use_name_as_value = true

saves the value :id in the database but activeadmin displays value :name as "empty".

  • Activeadmin filter :as => :select doesn't work. :as => :search works without issues.

Upvotes: 0

Related Questions