3

I am using a word to category mapping. A word can be mapped to a category or a type. In database, I have words table, categories table, types table and a join mappings called word_categories which contains the word_id, cat_type (having value either category or type), cat_id (if the cat_type is category the cat_id corresponds to categories table, or if the cat_type is type the cat_id corresponds to the type table).

In active admin keyword_categories.rb i have

form do |f|
  f.inputs do
    if f.object.new_record?
      f.input :word, as: :select, collection: Word.unmapped.pluck(:word)
      f.input :cat_type, as: :select, :collection => ["Type", "Category"]
      if true #here i have to give some logic to select on the basis of the value selected in the first drop down
        f.input :cat_id, as: :select, :collection => Type.pluck(:name)
      else 
        f.input :cat_id, as: :select, :collection => Category.pluck(:name)
      end
    end
  end
  f.actions
end

I followed SO Questions, but i dont understand, how do i return value from the controller to the active admin rb file

I am a newbie to jquery

asimhashmi
  • 4,258
  • 1
  • 14
  • 34
inquisitive
  • 3,738
  • 6
  • 30
  • 56

1 Answers1

0

The ERB code that's in the view only gets run when the page is first loaded. You can't use it to build conditionals based on the live value of something in the page.

Long story short, the whole if block needs to go into your javascript. It'll look something like:

<script>
  if($("object_cat_type").value() === "Category") {
    $("object_cat_id").innerHTML("<%= options_from_collection_for_select(Category.pluck(:name)) %>");
  } else if($("object_cat_type").value() === "Type") {
    $("object_cat_id").innerHTML("<%= options_from_collection_for_select(Type.pluck(:name)) %>");
  }
</script>

My jQuery is also a little rusty, though, so double check stuff against the documentation, especially that innerHTML function.

PJSCopeland
  • 2,818
  • 1
  • 26
  • 40