# Acts\_as\_taggable confused

**URL:** <https://www.ruby-forum.com/t/acts-as-taggable-confused/93975>\
**Category:** Rails\
**Created:** [April 17, 2006, 7:33am UTC](https://www.ruby-forum.com/t/acts-as-taggable-confused/93975 "2006-04-17T07:33:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 17, 2006, 7:33am UTC](https://www.ruby-forum.com/t/acts-as-taggable-confused/93975/1 "2006-04-17T07:33:45Z")

</div>

Ahoy,

So i’ve installed the acts\_as\_taggable module and everything is fine,  
but i’m a bit confused about this bit of code described in the API

"photo = Photo.new

# splits and adds to the tags collection

photo.tag “wine beer alcohol”

# don’t need to split since it’s an array, but replaces the tags

collection

# trailing and leading spaces are properly removed

photo.tag ['wine ‘, ’ vodka’], :clear =\> true

photo.tag\_names # =\> [‘wine’, ‘vodka’]

# appends new tags with a different separator

# the ‘wine’ tag wonÂ´t be duplicated

photo.tag\_names \<\< ‘wine, beer, alcohol’, :separator =\> ‘,’

# The difference between +tag\_names+ and +tags+ is that +tag\_names+

# holds an array of String objects, mapped from +tags+, while +tags+

# holds the actual +has\_and\_belongs\_to\_many+ collection, and so, is

# composed of +Tag+ objects.

photo.tag\_names.size # =\> 4  
photo.tags.size # =\> 4  
"

How do i actually rig this up to my \_form.rhtml so that I can add tags  
to an item? The above code only seems to give static tags such as “wine  
beer alchohol”

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 17, 2006, 9:38am UTC](https://www.ruby-forum.com/t/acts-as-taggable-confused/93975/2 "2006-04-17T09:38:33Z")

</div>

Thanks again Jeff, you’re the man.

I had actually figured it out and came to post my findings 🙂

In my actions NEW and EDIT \_form.rhtml I added a field for tags

Tags for this Item  
\<%= text\_field\_tag( 'tags', '', "value" =\> "#{h @item.tag\_names.join(" ")}") %\>

I ran into some stringify\_keys! issues, which is why it’s done this way.

And my items\_controller was updated with an additional line to save the  
tags

def update  
@item = Item.find(params[:id])  
@item.tag(params[:tags])  
if @item.update\_attributes(params[:item])  
flash[:notice] = ‘Item was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @item  
else  
render :action =\> ‘edit’  
end  
end

Perhaps I should ad this to the wiki…

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 17, 2006, 10:09am UTC](https://www.ruby-forum.com/t/acts-as-taggable-confused/93975/3 "2006-04-17T10:09:33Z")

</div>

Will Jessup wrote:

> Thanks again Jeff, you’re the man.
> 
> I had actually figured it out and came to post my findings 🙂
> 
> In my actions NEW and EDIT \_form.rhtml I added a field for tags
> 
> Tags for this Item  
> \<%= text\_field\_tag( 'tags', '', "value" =\> "#{h @item.tag\_names.join(" ")}") %\>
> 
> I ran into some stringify\_keys! issues, which is why it’s done this way.
> 
> And my items\_controller was updated with an additional line to save the  
> tags
> 
> def update  
> @item = Item.find(params[:id])  
> @item.tag(params[:tags])  
> if @item.update\_attributes(params[:item])  
> flash[:notice] = ‘Item was successfully updated.’  
> redirect\_to :action =\> ‘show’, :id =\> @item  
> else  
> render :action =\> ‘edit’  
> end  
> end
> 
> Perhaps I should ad this to the wiki…

Nice work!

One comment–

text\_field\_tag(name, value = nil, options = {})

The second parameter sets the starting value of the text field tag. Is  
that what you’re doing with your third param?

text\_field\_tag ‘tags’, h(@item.tag\_names.join(" \> "))

might be a more direct way of doing it. Or does that have something to  
do with the stringify\_keys issue you mentioned?

Jeff C.man

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 17, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-confused/93975/4 "2006-04-17T22:48:51Z")

</div>

I did it that way because in the documentation, the 2nd variable does  
something like

text\_field\_tag(one,two) # =\>

But i need to also attach the .join(" ") and it wasn’t working out, so I  
just set the value w/ the options parameter.

Will try your method and report back.

> Nice work!
> 
> One comment–
> 
> text\_field\_tag(name, value = nil, options = {})
> 
> The second parameter sets the starting value of the text field tag. Is  
> that what you’re doing with your third param?
> 
> text\_field\_tag ‘tags’, h(@item.tag\_names.join(" \> "))
> 
> might be a more direct way of doing it. Or does that have something to  
> do with the stringify\_keys issue you mentioned?
> 
> Jeff C.man

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 17, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-confused/93975/5 "2006-04-17T22:52:51Z")

</div>

\<%= text\_field\_tag ‘tags’, h(@item.tag\_names.join(" ")) %\>

works great!

Why did you have a .join(" \> “) instead of .join(” ") ?

Thanks

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 17, 2006, 8:50am UTC](https://www.ruby-forum.com/t/acts-as-taggable-confused/93975/6 "2006-04-17T08:50:22Z")

</div>

Will Jessup wrote:

> Ahoy,
> 
> So i’ve installed the acts\_as\_taggable module and everything is fine,  
> but i’m a bit confused about this bit of code described in the API
> 
> "photo = Photo.new
> 
> # splits and adds to the tags collection
> 
> photo.tag “wine beer alcohol”
> 
> # don’t need to split since it’s an array, but replaces the tags
> 
> collection
> 
> # trailing and leading spaces are properly removed
> 
> photo.tag ['wine ‘, ’ vodka’], :clear =\> true
> 
> photo.tag\_names # =\> [‘wine’, ‘vodka’]
> 
> # appends new tags with a different separator
> 
> # the ‘wine’ tag wonÂ´t be duplicated
> 
> photo.tag\_names \<\< ‘wine, beer, alcohol’, :separator =\> ‘,’
> 
> # The difference between +tag\_names+ and +tags+ is that +tag\_names+
> 
> # holds an array of String objects, mapped from +tags+, while +tags+
> 
> # holds the actual +has\_and\_belongs\_to\_many+ collection, and so, is
> 
> # composed of +Tag+ objects.
> 
> photo.tag\_names.size # =\> 4  
> photo.tags.size # =\> 4  
> "
> 
> How do i actually rig this up to my \_form.rhtml so that I can add tags  
> to an item? The above code only seems to give static tags such as “wine  
> beer alchohol”

I haven’t used acts\_as\_taggable, but one way to do it should be to  
create a text field (perhaps done with text\_field\_tag rather than  
text\_field) called “tags”, then you’d handle the new tags in your create  
and/or controller:

@item.tag params[:tags] unless params[:tags].blank?

This would add the new tags to your model @item (obviously replace this  
with your proper model name) if the tags field wasn’t empty.

There may be a simpler way, but this is what looks good to me.

Jeff C.man

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 18, 2006, 4:14am UTC](https://www.ruby-forum.com/t/acts-as-taggable-confused/93975/7 "2006-04-18T04:14:07Z")

</div>

Will Jessup wrote:

> \<%= text\_field\_tag ‘tags’, h(@item.tag\_names.join(" ")) %\>
> 
> works great!
> 
> Why did you have a .join(" \> “) instead of .join(” ") ?
> 
> Thanks

Must have been just an artifact of copying quoted text. I think the  
join came at the end of the line, and the \> appeared at the start of the  
quoted line. I thought that was odd at the time but didn’t look closely  
enough at it to take it out. 🙂

Glad it worked for you!

Jeff C.man
