我想使用acts_as_taggable_on为公司模型分配两种不同的“类型”标签(扇区类别和自由标签)。注意:我是RoR的新手!如何使用复选框为acts_as_taggable_on应用标签?
这很容易做,如果只是使用标准的文本输入字段,但我想对一种类型(预定义的固定扇区类别标签)使用复选框,然后允许用户添加逗号分隔标签在输入字段中。
我已经发挥各地以各种方式这个问题,...一个由this question启发......但我不能让它工作
这是我到目前为止有:
# 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宝石)我有...
# 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"
在我公司控制器我有
# 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
谁能暗示我怎么能做到这一点吗?
一个相关的问题是,如果这是一个很好的方法来做到这一点?使用类别模型通过联合模型分配扇区标签会更好吗?
谢谢!
你的方法可以是这样的:def get_sectors; Company.sector_counts.collect(:名称);结束 – 2011-07-19 15:14:15