【问题标题】:undefined method `has_one_attached' - Spree, AWS S3, Product Images, Rails未定义的方法 `has_one_attached' - Spree、AWS S3、产品图片、Rails
【发布时间】:2018-06-26 00:17:51
【问题描述】:

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

有没有人遇到过这个错误并有解决办法?

【问题讨论】:

  • 有同样的问题。有人可以帮忙吗?
  • 你找到解决办法了吗?
  • 有没有人设法解决这个问题?

标签: ruby-on-rails activerecord amazon-s3 spree ruby-on-rails-5.2


【解决方案1】:

spree.rb 中的注释行 attachment_config

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

亚马逊: 服务:S3 access_key_id:“” 秘密访问密钥:“” 桶: ”” 地区:“”#例如'我们-东-1'

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

config.active_storage.service = :amazon

aws-sdk-s3 gem 添加到您的 Gemfile:

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

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

【讨论】:

    【解决方案2】:

    对于将来遇到此问题的任何人,这就是我解决的方法。

    如果您正在编辑 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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多