Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update owner tags via form

I would like to uniquely use owner tags in my app. My problem is that when I create / update a post via a form I only have f.text_field :tag_list which only updates the tags for the post but has no owner. If I use f.text_field :all_tags_list it doesn't know the attribute on create / update. I could add in my controller:

User.find(:first).tag( @post, :with => params[:post][:tag_list], :on => :tags )

but then I have duplicate tags, for post and for the owner tags. How can I just work with owner tags?

like image 953
Thomas Traum Avatar asked Sep 01 '10 14:09

Thomas Traum


2 Answers

The answer proposed by customersure (tsdbrown on SO) on https://github.com/mbleigh/acts-as-taggable-on/issues/111 works for me

# In a taggable model:
before_save :set_tag_owner
def set_tag_owner
    # Set the owner of some tags based on the current tag_list
    set_owner_tag_list_on(account, :tags, self.tag_list)
    # Clear the list so we don't get duplicate taggings
    self.tag_list = nil
 end

# In the view:
<%= f.text_field :tag_list, :value => @obj.all_tags_list %>
like image 190
Guillaume Besse Avatar answered Oct 15 '22 06:10

Guillaume Besse


I used an observer to solve this. Something like:

in /app/models/tagging_observer.rb

class TaggingObserver < ActiveRecord::Observer
  observe ActsAsTaggableOn::Tagging

  def before_save(tagging)
    tagging.tagger = tagging.taggable.user if (tagging.taggable.respond_to?(:user) and tagging.tagger != tagging.taggable.user)
  end
end

Don't forget to declare your observer in application.rb

config.active_record.observers = :tagging_observer
like image 45
Yannis Avatar answered Oct 15 '22 06:10

Yannis