Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Admin has_many selectable list of records

I have been trying for days now, I am new at ROR and active admin. So far I have been able to add and delete has_many relations for a new record. And I am using strong_parameters as well as accept_nested_attributes. I want the

  • Ability to add and delete relations for existing records as well.

Ideally there should be an autocomplete box that allows searching and selection of an existing meaning for this particular model.

My models are

  • Word
  • Meaning
  • WordMeaning

I only want the capability to attach meanings that are already available to a word?

      class Word < ActiveRecord::Base    
         belongs_to :language    
         has_many :word_meanings
         has_many :meanings ,through: :word_meanings

form do |f|

f.semantic_errors *f.object.errors.keys
f.inputs do
  f.input :language
  f.input :word
  f.input :wordInScript
  f.input :pronunciation, :required => false, :as => :file
end

f.inputs do
  f.has_many :meanings, heading: 'Meanings', allow_destroy: true, new_record: true do |a|
    a.input :meaning
    a.input :language
  end
end

f.actions
end

like image 959
JehandadK Avatar asked Jul 28 '14 08:07

JehandadK


1 Answers

You can determine the collection of the select:

a.input :meaning, :as => :select, :collection => {#your collection in Hash, Array or ActiveRecord relation}.map{|key, value| [value, key] }

ActiveAdmin uses Formtastic: https://github.com/justinfrench/formtastic#usage

like image 110
nistvan Avatar answered Oct 18 '22 11:10

nistvan