Ruby on Rails
Acts As Taggable Gem (Version #16)

There are two versions of acts_as_taggable: More information on the Gem can be found here: Issues:

This is an example how to add / edit / list tags

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



<= 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

and in the list:

tags for this Item: <=h @item.tag_names.join(” “) >



The problem with the above code is that you cannot replace tags, only add them. If you would like a tag field type of set up, do this:
class Item <ActiveRecord::Base
  def tag_text
    tag_names.join(" ")
  end
  def tag_text=(tags)
    tag(tags, :clear => true)
  end
end

Then all you need to do in the RHTML to add the field:

<= text_field 'item', 'text_tags' >

And to show:

Tags: <=h @item.text_tags >

This keeps tag hanlding transparent and out of the controller, too.

There are two versions of acts_as_taggable: More information on the Gem can be found here: Issues:

This is an example how to add / edit / list tags

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



<= 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

and in the list:

tags for this Item: <=h @item.tag_names.join(” “) >



The problem with the above code is that you cannot replace tags, only add them. If you would like a tag field type of set up, do this:
class Item <ActiveRecord::Base
  def tag_text
    tag_names.join(" ")
  end
  def tag_text=(tags)
    tag(tags, :clear => true)
  end
end

Then all you need to do in the RHTML to add the field:

<= text_field 'item', 'text_tags' >

And to show:

Tags: <=h @item.text_tags >

This keeps tag hanlding transparent and out of the controller, too.