Skip to content

Instantly share code, notes, and snippets.

@bluengreen
Created November 6, 2018 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluengreen/c4833f5cfb161d4599f81669c5fa441d to your computer and use it in GitHub Desktop.
Save bluengreen/c4833f5cfb161d4599f81669c5fa441d to your computer and use it in GitHub Desktop.
Integrate acts-as-taggable-on with activeadmin

model

class Product < ActiveRecord::Base
  acts_as_taggable_on :categories, :tag
  ....

activeadmin

form

f.input :tag_list, :input_html => {:value => f.object.tag_list.join(", ") }, :label => "Tags (separated by commas)".html_safe
f.inputs "Existing Categories(現有分類): #{exsisting_categories.to_sentence}" do

f.input :category_list, :as => :check_boxes, \
  :collection => Category.all.map { |category| [category.name, category.id, {:checked => (category.name.in? f.object.category_list) }] }
end

save the tag

before_create do |product|
  categories = []
  params["product"]["category_list"].reject{ |c| c.empty? }.each do |category|
    categories << Category.find_by_id(category.to_i).name
  end
  product.category_list = categories
  product.tag_list = params["product"]["tag_list"]
end

before_update do |product|
  categories = []
  params["product"]["category_list"].reject{ |c| c.empty? }.each do |category|
    categories << Category.find_by_id(category.to_i).name
  end
  product.category_list = categories
  product.tag_list = params["product"]["tag_list"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment