How to apply tags with acts_as_taggable_on using check boxes?

前端 未结 2 804
借酒劲吻你
借酒劲吻你 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
    
    0 讨论(0)
  • 2020-12-24 10:42

    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 %>
    
    0 讨论(0)
提交回复
热议问题