How to apply tags with acts_as_taggable_on using check boxes?

六眼飞鱼酱① 提交于 2019-11-29 23:23:28

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...

In my company model

# models/company.rb
class Company ...
  acts_as_taggable_on :tags, :sectors
...
end

in the form

# 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

It seems that act_as_taggable_on uses single table inheritance, so you actually don't need to create any additional tables. However you do need to follow their convention (that they never stated) as follows:

//add to model
attr_accessible :yourfieldname_list
acts_as_taggable_on :yourfieldname

//view
<%= f.text_field :yourfieldname_list %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!