如何使用复选框应用带有acts_as_taggable_on 的标签? [英] How to apply tags with acts_as_taggable_on using check boxes?
问题描述
我想使用 acts_as_taggable_on.注意:我是 RoR 的新手!
I would like to assign two different "types" of tags (sector categories and free tagging) to a Company model using acts_as_taggable_on. NB: I'm new to RoR!
如果只使用标准文本输入字段,这很容易做到,但我想在一种类型(预定义的固定扇区类别标签)上使用复选框,然后允许用户添加逗号分隔的标签一个输入字段.
This is easy to do if just using standard text input fields, but I would like to use check-boxes on one type (a fixed sector category tag that is predefined), and then allow the user to add comma separated tags in an input field.
我以各种方式解决了这个问题,...一个灵感来自 这个问题...但我无法让它工作
I have played around with this problem in various ways,... one inspired by this question...but I cannot get it to work
这是我目前所拥有的:
# models/company.rb
class Company ...
acts_as_taggable_on :tags, :sectors
has_many :taggings,
:as => :taggable,
:include => :tag,
:class_name => "ActsAsTaggableOn::Tagging",
:conditions => { :taggable_type => "Company" }
has_many :sector_tags,
:through => :taggings,
:source => :tag,
:class_name => "ActsAsTaggableOn::Tag",
:conditions => {:context => "sectors"}
end
在表单中(使用 simple_form gem)我有...
in the form (using simple_form gem) I have...
# views/companies/_form.html.haml
= simple_form_for @company do |f|
= f.input :name
= f.association :sector_tags, :as => :check_boxes, :hint => "Please click all that apply"
= f.input :tag_list
= f.button :submit, "Add company"
在我的公司控制器中,我有
And in my Company controller I have
# controllers/companies_controller.rb
def create
@company = current_user.companies.build(params[:company])
if @company.save
...
end
但这会导致验证错误:
ActiveRecord::RecordInvalid in CompaniesController#create
Validation failed: Context can't be blank
谁能暗示我如何正确地做到这一点?
Can anyone hint at how I can do this right?
一个相关的问题是,这是否是一个好方法?仅使用类别模型通过联合模型分配扇区标签会更好吗?
A related question is if this is a good way to do it at all? Would I be better off just using a Category model for assigning sector tags through a joint model?
谢谢!
推荐答案
好吧,我解决了我的问题.结果证明这很简单.唉,我最终通过联合部门化"表创建了一个单独的部门模型.但如果有人感兴趣,我只想更新我在上述案例中所做的......
Well, I solved my problem. And it turned out to be quite simple. Alas, I ended up creating a separate Sector model through a joint "sectorizations" table. But if anyone is interested, I just wanted to update on what I did in the case above...
在我的公司模型中
# models/company.rb
class Company ...
acts_as_taggable_on :tags, :sectors
...
end
在表格中
# views/companies/_form.html.haml
= simple_form_for @company do |f|
= f.input :name
= f.input :sector_list, :as => :check_boxes, :collection => @sectors, :hint => "Please check all that apply"
= f.input :tag_list
= f.button :submit, "Add company"
并在公司控制器中(创建)
and in the company controller (create)
# controllers/company_controllers.rb
def new
@company = Company.new
@sectors = get_sectors
end
def get_sectors
sectors = []
for sector in Company.sector_counts
sectors << sector['name']
end
return sectors
end
这篇关于如何使用复选框应用带有acts_as_taggable_on 的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!