Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum selection in ActiveAdmin for a Engine Model

I continue to get a undefined method error when attempting to get the ActiveAdmin form to use a selection box based of the keys for an enum properties for an object. Following the suggested method on stack overflow to configure active admin forms for enums seems to get me half way there although the engine aspect of my object seems to make the enum attribute through the NoMethodError exception.

 #/lib/book_store/admin/books
 if defined?(ActiveAdmin)
  ActiveAdmin.register  BookStore::Book, as: 'Book' do
    # customize your resource here
    form do |f|
      f.semantic_errors # shows errors on :base
      f.inputs   do
        f.input :cover_type, as: :select, collection:  BookStore::Book.cover_type.keys
      end
      f.actions         # adds the 'Submit' and 'Cancel' buttons
    end
    permit_params :name, :lead, :excerpt, :description, :price, :slug, :cover_type, :num_pages, :size, :cover_image, :author, :author_id, :category
  end
end



#app/models/book_store/book.rb
module BookStore
  class Book < ActiveRecord::Base
    belongs_to :author
    belongs_to :category
    enum cover_type: [:soft, :hard]
  end
end

gets the following error

undefined method `cover_type' for #<Class:0x007fe685217af0>

here is the full stack trace

like image 206
DogEatDog Avatar asked Jan 29 '26 12:01

DogEatDog


2 Answers

I think cover_type should be pluralized.

f.input :cover_type, as: :select, collection:  BookStore::Book.cover_types.keys
like image 53
Molle Avatar answered Feb 01 '26 06:02

Molle


Almost a related answer...

You can use enumerize gem and activeadmin addons to handle enumerations nicely.

like image 28
Leantraxxx Avatar answered Feb 01 '26 05:02

Leantraxxx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!