5

我正在尝试将我的 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'

配置/初始化程序/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

有没有人遇到过这个错误并有解决方案?

4

3 回答 3

0

attachment_configspree.rb 中的注释行

在 .中声明 Active Storage 服务config/storage.yml。对于您的应用程序使用的每项服务,请提供名称和必要的配置。

亚马逊:
  服务:S3
  access_key_id:“”
  秘密访问密钥:“”
  桶: ””
  region: "" # 例如'us-east-1'

要在生产中使用 Amazon S3 服务,请将以下内容添加到config/environments/production.rb

config.active_storage.service = :amazon

aws-sdk-s3gem 添加到您的 Gemfile 中:

gem "aws-sdk-s3", require: false

来源:http ://edgeguides.rubyonrails.org/active_storage_overview.html#setup

于 2019-01-10T10:02:03.727 回答
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

于 2020-10-26T22:47:32.270 回答
-1

该配置对我也不起作用。其实步骤很简单。在 storage.yml 中声明 amazon 配置,添加 aws gem,然后就可以开始了。ActiveStorage 文档拥有一切。http://edgeguides.rubyonrails.org/active_storage_overview.html#setup

于 2018-07-20T19:41:57.033 回答