Search code examples
ruby-on-railspaperclipacts-as-taggable-onmass-assignment

Extending ActsAsTaggableOn to have Images, but getting "Can't mass-assign protected attributes: tag_image" Error


I'm using the acts_as_taggable_on gem to tag ads. It works fine, but I need the tags to have images, so I decided to extend the plugin and writed this module:

# Add logic to ActsAsTaggableOn Tag model
module TagExtend

  def self.included(recipient)
    recipient.extend(ClassMethods)
  end
  module ClassMethods

    ActiveRecord::Base.attr_accessible :tag_image
    ActiveRecord::Base.has_attached_file :tag_image,
      :styles => { :medium => "300x200>"},
      :storage => :s3,
      :bucket => S3_BUCKET,   
      :s3_host_name =>  S3_HOST,
      :s3_credentials => {
        :access_key_id => S3_KEY,
        :secret_access_key =>S3_SECRET      
      }
  end

end

And in an initializer file:

require File.dirname(__FILE__) + '/../../lib/tag_extend.rb'
ActsAsTaggableOn::Tag.send(:include, TagExtend)

It should work, but when I try to save a Tag with an image (from ActiveAdmin) I'm getting: Can't mass-assign protected attributes: tag_image

Any suggestion on this ?


Solution

  • if i am not confused, you need to call attr_accessible on the specific class and not on the base class ActiveRecord::Base.

    so it would be:

    module TagExtend
      def self.included(clazz)
        clazz.attr_accessible :tag_image
        clazz.has_attached_file :tag_image, {...}
      end
    end
    

    and you should NOT do stuff like require File.dirname(__FILE__) + '/../../lib/tag_extend.rb' always respect the load-path!

    so use require 'tag_extend' and add lib to your autoload-path if you are using rails.