5

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

Pinser
  • 1,898
  • 3
  • 29
  • 43

1 Answers1

6

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

nistvan
  • 2,940
  • 1
  • 16
  • 20
  • Ok should I remove "a.input :language" then – Pinser Jul 28 '14 at 10:30
  • But it is showing ids of meanings, I have 50000rows in my actual db. How can I manage that? – Pinser Jul 28 '14 at 10:31
  • Not working! also can I retain the ability to create new meanings along with this? – Pinser Jul 28 '14 at 10:41
  • your_collectoin.map{|key, value| [value, key] } -> in that case not the id will be appear – nistvan Jul 28 '14 at 10:50
  • when you have so many records, you can use ajax with select2 for example – nistvan Jul 28 '14 at 10:51
  • I am sorry, I am absolute newbie, Can you update the answer with the example value,key u mentioned. Thanks! – Pinser Jul 28 '14 at 10:54
  • As I understand from here https://github.com/gregbell/active_admin/issues/3039 . The select only gives a list of options to fill but I want a complete relations. A word related to an existing meaning which already has all the fields. – Pinser Jul 28 '14 at 11:06
  • I used something like this Meaning.all.map { |u| ["#{u.meaning}", u.id] } But new records are created with ids as their meanings. I want to ref existing records :( – Pinser Jul 28 '14 at 11:34
  • Let be all the fields of Meaning select2 input, and filter the collection of select via ajax. Those select2 inputs should be in a has_many form. – nistvan Jul 28 '14 at 18:21
  • I am going to try this today! Thanks for the hint – Pinser Aug 05 '14 at 08:40