How to apply tags with acts_as_taggable_on using check boxes?

前端 未结 2 809
借酒劲吻你
借酒劲吻你 2020-12-24 10:01

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 i

2条回答
  •  醉酒成梦
    2020-12-24 10:27

    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
    

提交回复
热议问题