【问题标题】:Adding Paperclip Attachment to Spree Orders Table,在 Spree Orders 表中添加回形针附件,
【发布时间】:2020-09-04 08:59:40
【问题描述】:

我正在使用 Solidus、Rails 开发电子商务网站。该网站允许您从各种选项中订购相框和打印件。 要打印照片,用户必须上传照片的 jpg 文件。因此,为了允许我修改了订单表并添加了一个名为“附件”的回形针附件

我运行了以下命令

rails generate paperclip SpreeOrder attachment

生成迁移,然后我运行rake db:migrate

然后我创建了一个 spree/order_decorator.rb 文件,并添加了has_attached_file

 module Spree::OrderDecorator
  has_attached_file :attachment, styles: {
      :medium => {
          :geometry => "640x480",
          :format => 'jpeg'
      },
      :thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
  }, :processors => [:transcoder]

  validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/

  Spree::Order.prepend self
end

在这之后我运行了服务器,结果出现了这个错误

 undefined method `has_attached_file' for Spree::OrderDecorator:Module (NoMethodError)

我已将solidus 配置为仅与回形针一起使用,所以我真的很困惑为什么会收到此错误,甚至后来我手动在 config/initializers 目录中生成了一个 paperclip.rb 文件,但我仍然得到同样的错误。

请帮助解决这个问题! 谢谢!!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 spree ruby-on-rails-6 solidus


    【解决方案1】:

    您应该在前置模块的类级别添加这些回形针方法:

    def self.prepended(base)
      base.has_attached_file 
    end
    

    【讨论】:

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