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(” “) >
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.
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(” “) >
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.