Change default delimiter using action-like-taggable-on

The default separator in acts-as-taggable-on is a comma. I would like to change this to a space in a Rails 3 application. For example, tag_list should be assigned as follows:

object.tag_list = "tagone tagtwo tagthree"

not like that:

object.tag_list = "tagone, tagtwo, tagthree"

What is the best way to change the delimiter?

+5
source share
2 answers

You need to define the separator class variable in the class ActsAsTaggableOn :: TagList

In the initializer add:

ActsAsTaggableOn::TagList.delimiter = ' '
+8
source

I would not hack inside act-as-taggable-on, just create another method for the class that implements it:

class MyClass < ActiveRecord::Base
  acts_as_taggable

  def human_tag_list
    self.tag_list.gsub(', ', ' ')
  end

  def human_tag_list= list_of_tags
    self.tag_list = list_of_tags.gsub(' ', ',')
  end
end

MyClass.get(1).tag_list # => "tagone, tagtwo, tagthree"
MyClass.get(1).human_tag_list # => "tagone and tagtwo and tagthree"
MyClass.get(1).human_tag_list = "tagone tagtwo tagthree"
+1
source

All Articles