Changing the default delimiter with acts-as-taggable-on
Asked Answered
K

2

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?

Keaton answered 4/1, 2011 at 8:8 Comment(0)
T
8

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

In an initializer add that :

ActsAsTaggableOn::TagList.delimiter = ' '
Turbinate answered 4/1, 2011 at 8:16 Comment(1)
in the newest version: ActsAsTaggableOn.delimiter = ' 'Rusty
D
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"
Dah answered 4/1, 2011 at 8:17 Comment(1)
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.Keaton

© 2022 - 2024 — McMap. All rights reserved.