Manipulating tags with acts_as_taggable_on and ActiveAdmin
I

1

12

I have a Post model which I'm accessing through ActiveAdmin. It's also taggable using the acts_as_taggable_on gem. So the admin can add, edit or delete tags from a specific Post.

The normal way to add the tagging functionality for the resource in your admin panel is by doing this in admin/posts.rb:

ActiveAdmin.register Post do
  form do |f|
    f.inputs "Details", :multipart => true do
      f.input :tag_list
      # and the other irrelevant fields goes here
    end

    f.buttons
  end
end

However, I want to have the tags selected from a multiple select form field and not being entered manually in a text field (like it is with the code above). So I've tried doing this:

      f.input :tag_list, :as => :select,
                         :multiple => :true,
                         :collection => ActsAsTaggableOn::Tag.all

but it doesn't work as expected. This actually creates new tags with some integer values for names and assigns them to that Post. Someone told me that extra code is needed for this to work.

Any clues on how this is done? Here's my model just in case: http://pastie.org/3911123

Thanks in advance.

Intraatomic answered 14/5, 2012 at 18:7 Comment(1)
I am trying to do the same thing, but because actsastaggable on requires a string seperated by commas, I can't get strong parameters to accept the array that a multi-select dropdown gives. How did you handle this?Eckhardt
D
12

Instead of

:collection => ActsAsTaggableOn::Tag.all

try

:collection => ActsAsTaggableOn::Tag.pluck(:name)

Setting the collection to Tag.all is going to tag your posts with the tag's ID, since that's how tags are identified by default (that's where the integer values for names are coming from). map(&:name) tells the form builder to use the tag's name instead.

Disclamation answered 24/11, 2012 at 3:4 Comment(1)
ActsAsTaggableOn::Tag.pluck(:name) would be even better because it won't load all the tags into memory before extracting the name.Permanency

© 2022 - 2024 — McMap. All rights reserved.