我试图将我的Spree购物车与AWS S3连接以上传产品图片,但是我一直收到错误消息:
.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.2.0/lib/active_record/dynamic_matchers.rb:22:in `m
ethod_missing': undefined method `has_one_attached'
这是我的设置:
宝石文件
ruby '2.4.0'
gem 'rails', '~> 5.2.0'
gem 'spree', '~> 3.6.0'
gem 'spree_auth_devise', '~> 3.3'
gem 'spree_gateway', '~> 3.3'
gem 'globalize', github: 'globalize/globalize'
gem 'spree_i18n', github: 'spree-contrib/spree_i18n'
gem 'spree_globalize', github: 'spree-contrib/spree_globalize', branch: 'master'
gem 'spree_static_content', github: 'spree-contrib/spree_static_content'
gem 'aws-sdk', '~> 2.3'
config / initializers / spree.rb
attachment_config = {
s3_credentials: {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
bucket: ENV['S3_BUCKET_NAME']
},
storage: :s3,
s3_region: ENV['S3_REGION'],
s3_headers: { "Cache-Control" => "max-age=31557600" },
s3_protocol: "https",
bucket: ENV['S3_BUCKET_NAME'],
url: ":s3_domain_url",
styles: {
mini: "48x48>",
small: "100x100>",
product: "240x240>",
large: "600x600>"
},
path: "/:class/:id/:style/:basename.:extension",
default_url: "/:class/:id/:style/:basename.:extension",
default_style: "product"
}
attachment_config.each do |key, value|
Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
end
有人遇到这个错误并有解决方案吗?
答案 0 :(得分:0)
spree.rb中的注释行attachment_config
在
config/storage.yml
中声明Active Storage服务。对于您的应用程序使用的每项服务,请提供名称和必要的配置。
amazon: service: S3 access_key_id: "" secret_access_key: "" bucket: "" region: "" # e.g. 'us-east-1'
要在生产中使用Amazon S3服务,请将以下内容添加到
config/environments/production.rb
:
config.active_storage.service = :amazon
将
aws-sdk-s3
宝石添加到您的Gemfile中:
gem "aws-sdk-s3", require: false
来源:http://edgeguides.rubyonrails.org/active_storage_overview.html#setup
答案 1 :(得分:0)
对于以后遇到此问题的任何人,这就是我已解决的方法。
如果您将Spree :: User类作为初始化程序进行编辑,则问题在于'active_storage / reflection'中的初始化程序未运行。因此,请在您的class_eval块的开头添加以下行:
include ActiveStorage::Reflection::ActiveRecordExtensions
ActiveRecord::Reflection.singleton_class.prepend(ActiveStorage::Reflection::ReflectionExtension)
include ActiveStorage::Attached::Model
然后,应加载ActiveStorage,您将能够找到方法has_one_attached :image
答案 2 :(得分:-1)
该配置也不适用于我。实际上,步骤非常简单。 在storage.yml中声明Amazon配置,添加aws gem,就可以开始了。 ActiveStorage文档包含了所有内容。 http://edgeguides.rubyonrails.org/active_storage_overview.html#setup