Open wuyuedefeng opened 4 years ago
gem reform
更新attachment
app/forms/reform_base.rb
class ReformBase < Reform::Form
# https://edgeguides.rubyonrails.org/active_storage_overview.html
def attach_file property
if self.try(property).present?
self.model.try(property).attach self.try(property)
elsif self.model.try(property).try(:attached?)
self.model.try(property).purge_later
end
true
end
end
app/forms/api/banner_form/create.rb
module BannerForm
class Create < ReformBase
model :banner
property :image, virtual: true # signed_blob_id
validates :image, presence: true
def save
attach_file :image if super
end
end
end
app/forms/api/banner_form/update.rb
module BannerForm
class Update < ReformBase
model :banner
property :image, virtual: true # signed_blob_id
validates :image, presence: true
def save
attach_file :image if super
end
end
end
module Api::FeedbackForm
class Create < ReformBase
model :feedback
property :images # 直接将signed_ids数组赋值给images即可,更新也是一样
end
end
active_storage
in model
默认
storage
以文件的形式存在服务器中storage
存储方式到阿里云 gemfile添加 gemactivestorage-aliyun
修改
config/storage.yml
文件配置:local: service: Disk root: <%= Rails.root.join("storage") %>
aliyun: service: Aliyun access_key_id: "LTAIwdxxxxxxxx" access_key_secret: "juYPTswGjxxxxxxxxxxxxxxx" bucket: "xxxx" endpoint: "https://oss-cn-hangzhou.aliyuncs.com"
path prefix, default: /
path: "moon"
Bucket mode: [public, private], default: public
mode: "public"
rails g controller uploader file
config/router.rb
添加路由配置eg: model
feedback.rb
usage, eg:
eg: model
feedback.rb
usage, eg: