mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.97k stars 1.2k forks source link

Does not populate tagger_id or tagger_type with devise current_user #370

Open 127 opened 11 years ago

127 commented 11 years ago

This case fails to add tagger_id and tagger_type

class User < ActiveRecord::Base
  acts_as_tagger
end

class Transaction < ActiveRecord::Base
  acts_as_taggable
end

module TransactionsHelper
  #makes no difference weather I use it or not
  include ActsAsTaggableOn::TagsHelper
end

view
 <%= f.text_field :tag_list %>

So, it looks like a bug. This case would work but will set dupes with tagger_id or tagger_type equal NULL .

       def create
        @transaction = current_user.transactions.build(params[:transaction])
        if params[:transaction][:tag_list].present? 
          current_user.tag(@transaction, :with => params[:transaction][:tag_list])
        end
      end

The solution is quite lame 1) Fixed form to accept raw params

     #it won't be populated with tags having tagger_id in the case of `f.input :tag_list` usage
     <%= text_field_tag :tag_list, @transaction.tags.join(', ') %>

2) Fixed controller (update for example)

  # POST /transactions
  # POST /transactions.json
  def create
    @transaction = current_user.transactions.build(params[:transaction])
    tagger

    respond_to do |format|
      if @transaction.save
        format.html { redirect_to @transaction, notice: 'Transaction was successfully created.' }
        format.json { render json: @transaction, status: :created, location: @transaction }
      else
        format.html { render action: "new" }
        format.json { render json: @transaction.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /transactions/1
  # PUT /transactions/1.json
  def update
    @transaction = current_user.transactions.find(params[:id])
    tagger

    respond_to do |format|
      if @transaction.update_attributes(params[:transaction])
        format.html { redirect_to @transaction, notice: 'Transaction was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @transaction.errors, status: :unprocessable_entity }
      end
    end
  end

  def tagger 
    if params[:tag_list].present? 
      current_user.tag(@transaction, :with => params[:tag_list], :on => :tags)
      #remove from params to prevent dupes with tagger_id and tagger_type = NULL
      params.delete('tag_list') 
    end
  end

Is there any simple way to use this gem with devise?

viruthagiri commented 10 years ago

+1

AlexVPopov commented 9 years ago

See my answer on StackOverflow to a similar question. Basically, just don't whitelist :tag_list and tag the object manually. So your create actions will look like this:

def create
  @photo = Photo.new(photo_params) # at this point @photo will not have any tags, because :tag_list is not whitelisted
  current_user.tag(@photo, on: :tags, with: params[:photo][:tag_list])

  if @photo.save
    redirect_to @photo
  else
    render :new
  end
end

and this will not create double taggings.