RailsCasts Pro episodes are now free!

Learn more or hide this

amalhotra's Profile

GitHub User: amalhotra

Comments by

Avatar

Quick and dirty way to even add new tags on the fly.

ruby
# Class that is taggable 
def tag_tokens=(ids)
    ids = ids.split(',')
    self.tag_list = Tag.get_tags_from_tokens(ids)
 end

# tag.rb
def self.get_tags_from_tokens(tokens)
    tags = []
    ids = []
    tokens.each do |token|
      if token =~ /<<<(.+?)>>>/
        tags << token.gsub(/<<<(.+?)>>>/, $1)
      else
        ids << token
      end
    end
    tags.concat(Tag.where(:id => ids).map(&:name)) unless ids.empty?
    tags.join(",")
 end

Maybe a better way is to just change the behavior on the client side?

Avatar

Missing this!

ruby
def self.ids_from_tokens(tokens)
    tokens.gsub!(/<<<(.+?)>>>/) { create!(name: $1).id }
    tokens.split(',')
  end

Got this from the source.