Saving tags with acts_as_taggable and activeadmin

3.3k Views Asked by At

I'm using ActiveAdmin and acts_as_taggable in a rails 3 app, and I can get the tag list to show fine as a checklist on edit pages, and I can add tags using the console and then remove them using the form, but it errors on saving the form if I try and add tags with

"Validation failed: Context can't be blank"

I only have one tagging context (tags).

ActiveAdmin form code is:

form    :html => { :multipart => true } do |f|
    f.inputs "Details" do
        f.input :title
        f.input :itinerary, :as => :select, :collection => Itinerary.all
        f.input :description
        f.input :address
        f.input :contact_details
        f.input :url
        f.input :phone
        f.input :nearest_tube
        f.input :timetable
        f.input :price
  f.input :tags, :as => :check_boxes, :multiple => true, :collection => @tags
        f.input :image, :as => :file
    end
    f.buttons
end

And in the model I have

class Ticket < ActiveRecord::Base
has_and_belongs_to_many :itinerary
acts_as_taggable_on :tags
has_attached_file :image, :styles => { :medium => "210x140>", :thumb => "100x100>" }  
end

If I add

  attr_writer :tag_ids

to the model, it no longer errors on saving, but still doesn't save the selected tags in the list.

Thanks!

2

There are 2 best solutions below

0
On

It doesn't use check boxes, but this worked well for me:

f.input :tag_list, :hint => 'Comma separated'
1
On

Taking inspiration from Nathan's answer, tag_list takes a list of tag names, so you can use checkboxes by passing a collection of tag names:

f.input :tag_list, :as => :check_boxes, 
  :collection => ActsAsTaggableOn::Tag.all.map(&:name)