5

The default delimiter in the acts-as-taggable-on gem is a comma. I'd like to change this to a space throughout my Rails 3 application. For example, tag_list should be assigned like this:

object.tag_list = "tagone tagtwo tagthree"

rather than like this:

object.tag_list = "tagone, tagtwo, tagthree"

What is the best way to go about changing the delimiter?

Chris Alley
  • 3,015
  • 2
  • 21
  • 31

2 Answers2

8

You need define the delimiter class variable in ActsAsTaggableOn::TagList class

In an initializer add that :

ActsAsTaggableOn::TagList.delimiter = ' '
shingara
  • 46,608
  • 11
  • 99
  • 105
1

I wouldn't go hacking around inside acts-as-taggable-on, just create another method on 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"
stef
  • 14,172
  • 2
  • 48
  • 70
  • This won't work for my application, since the user will be assigning the tag_list though a text field (e.g. `<%= f.text_field :tag_list %>`), and I want for them to be able to type spaces instead of commas to separate the tags. But this a good solution for handling the presentation of the tags after they've been created. – Chris Alley Jan 04 '11 at 08:44