How to apply tags with act_as_taggable_on using checkboxes?

I would like to assign two โ€œtagโ€ models (sector categories and free labeling) to company models using act_as_taggable_on . NB: I am new to RoR!

This is easy to do if you just use standard text input fields, but I would like to use checkboxes for one type (a predefined category tag of a fixed sector), and then allow the user to add tags separated by commas in the input field.

I played with this problem in different ways ... inspired by this question ... but I can't get it to work

Here is what I still have:

# 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 

in the form (using the pearl simple_form) 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

 # 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 how can I do this correctly?

A related question is, is this a good way to do this at all? Would I be better off using a category model to assign sector tags through a collaborative model?

Thanks!

+6
ruby-on-rails associations simple-form acts-as-taggable-on
source share
2 answers

Ok, I solved my problem. And that turned out to be pretty simple. Alas, I finished creating a separate sector model through a common โ€œsectorizationโ€ table. But if someone is interested, I just wanted to update what I did in the case above ...

In my company model

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

as

 # 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 
+7
source share

Act_as_taggable_on seems to use unidirectional table inheritance, so you don't need to create additional tables. However, you must follow their agreement (which they never made) as follows:

 //add to model attr_accessible :yourfieldname_list acts_as_taggable_on :yourfieldname //view <%= f.text_field :yourfieldname_list %> 
+1
source share

All Articles