2

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 ?

cicloon
  • 1,099
  • 5
  • 13

1 Answers1

2

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.

phoet
  • 18,688
  • 4
  • 46
  • 74
  • It solved my problem. Nevermind, I thought I had to call attr_accessible on the base class. Thanks for the answer !! – cicloon Mar 04 '12 at 16:20
  • this would implicate, that you only had one array of accessible attributes for all your model classes. that would not work very well. – phoet Mar 04 '12 at 16:57