# Hi, how can i rewrite this to take out commas in tag field?

**URL:** <https://www.ruby-forum.com/t/hi-how-can-i-rewrite-this-to-take-out-commas-in-tag-field/105315>\
**Category:** Rails\
**Created:** [May 28, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/hi-how-can-i-rewrite-this-to-take-out-commas-in-tag-field/105315 "2007-05-28T19:30:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [May 28, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/hi-how-can-i-rewrite-this-to-take-out-commas-in-tag-field/105315/1 "2007-05-28T19:30:48Z")

</div>

def tag\_with tags  
tags.split(" ").each do |tag|  
Tag.find\_or\_create\_by\_name(tag).taggables \<\< self  
end

i have a text field box where the user can create tags. however, if they  
use commas, the comma becomes part of the tag name. how can i prevent  
this from happening.

before i call the tag\_with, do a remove ‘,’?

thanks

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [May 28, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/hi-how-can-i-rewrite-this-to-take-out-commas-in-tag-field/105315/2 "2007-05-28T20:34:02Z")

</div>

mixplate wrote:

> i have a text field box where the user can create tags.  
> however, if they use commas, the comma becomes  
> part of the tag name. how can i prevent this from  
> happening.
> 
> before i call the tag\_with, do a remove ‘,’?

tags.gsub!(/’,’,’ '/) will substitute a space for a comma.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [May 28, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/hi-how-can-i-rewrite-this-to-take-out-commas-in-tag-field/105315/3 "2007-05-28T20:43:03Z")

</div>

Oops. Should be:

tags.gsub!(/‘,’/, ’ ')

----- Original Message -----  
From: “Bill W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, May 28, 2007 1:33 PM  
Subject: [Rails] Re: hi, how can i rewrite this to take out commas in  
tag  
field?

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [May 29, 2007, 2:46am UTC](https://www.ruby-forum.com/t/hi-how-can-i-rewrite-this-to-take-out-commas-in-tag-field/105315/4 "2007-05-29T02:46:51Z")

</div>

I prefer not to create a table just for tags. I just put a ‘tags’ column  
on the table and do this in my view:

@model.tags.split(’,’).compact.collect{|t|  
link\_to(h(t.strip),"/tags/#{CGI::escape(t.strip)}") }.join(’, ')

test it out on a string

“tag,tagging, , #_#@_(_(_!~{}|, tagged”.split(’,’).compact.collect{|t|  
link\_to(h(t.strip),"/tags/#{CGI::escape(t.strip)}") }.join(’, ')
