2

我正在使用acts_as_taggable_on gem 来标记广告。它工作正常,但我需要标签有图像,所以我决定扩展插件并编写这个模块:

# 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

在初始化文件中:

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

它应该可以工作,但是当我尝试使用图像(来自 ActiveAdmin)保存标签时,我得到:Can't mass-assign protected attributes: tag_image

对此有什么建议吗?

4

1 回答 1

2

如果我不感到困惑,您需要调用attr_accessible特定类而不是基类ActiveRecord::Base

所以它会是:

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

而且您不应该做任何事情,例如require File.dirname(__FILE__) + '/../../lib/tag_extend.rb'始终尊重加载路径!

因此,如果您使用的是 rails,请使用require 'tag_extend'并添加到您的自动加载路径中。lib

于 2012-03-04T16:00:02.447 回答