使用acts_as_taggable和activeadmin保存标签
我在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
到模型中,保存时不再出错,但仍然不会保存列表中选定的标签。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不使用复选框,但这对我来说效果很好:
It doesn't use check boxes, but this worked well for me:
受到 Nathan 答案的启发,tag_list 获取标签名称列表,因此您可以通过传递标签名称集合来使用复选框:
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: