如何使用acts_as_taggable_on使用复选框应用标签? [英] How to apply tags with acts_as_taggable_on using check boxes?
问题描述
我想使用的 acts_as_taggable_on 。注:我是新来的回报率
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!
这是很容易做到,如果仅仅使用标准文本输入字段,但我想用在一种类型(固定的行业类别标签是predefined)方框中打勾,然后允许用户添加逗号在输入字段分隔标签。
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.
我已经用各种方式这个问题发挥各地,......一个接<一个启发href=\"http://stackoverflow.com/questions/2082399/thinking-sphinx-and-acts-as-taggable-on-plugin\">this问题 ......但我不能让它开始工作。
I have played around with this problem in various ways,... one inspired by this question...but I cannot get it to work
下面是我到目前为止有:
Here is what I have so far:
# 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
但是,这将导致验证错误:
But this causes a validation error:
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?
谢谢!
推荐答案
好吧,我解决我的问题。它被证明是相当简单。唉,我结束了创建通过联合sectorizations表中单独的领域模型。但是,如果有人有兴趣,我只是想对我做了什么的情况下上述...
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屋!