Changing the default delimiter with acts-as-taggable-on

大憨熊 提交于 2019-12-10 13:56:29

问题


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?


回答1:


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

In an initializer add that :

ActsAsTaggableOn::TagList.delimiter = ' '



回答2:


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"


来源:https://stackoverflow.com/questions/4591648/changing-the-default-delimiter-with-acts-as-taggable-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!