2

我在rails 3应用程序中使用ActiveAdmin和acts_as_taggable,我可以让标签列表在编辑页面上显示为清单,我可以使用控制台添加标签,然后使用表单删除它们,但它会出错如果我尝试添加标签,则保存表单

“验证失败:上下文不能为空”

我只有一个标记上下文(标签)。

ActiveAdmin 表单代码为:

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

在我的模型中

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

如果我添加

  attr_writer :tag_ids

对于模型,它不再在保存时出错,但仍然不会将选定的标签保存在列表中。

谢谢!

4

2 回答 2

6

它不使用复选框,但这对我来说效果很好:

f.input :tag_list, :hint => 'Comma separated'
于 2012-01-19T22:59:31.063 回答
1

从 Nathan 的回答中汲取灵感, tag_list 采用标签名称列表,因此您可以通过传递标签名称集合来使用复选框:

f.input :tag_list, :as => :check_boxes, 
  :collection => ActsAsTaggableOn::Tag.all.map(&:name)
于 2013-03-07T05:59:10.393 回答