Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道5+;神龛多个文件上传_Ruby On Rails_Ruby_Shrine - Fatal编程技术网

Ruby on rails 轨道5+;神龛多个文件上传

Ruby on rails 轨道5+;神龛多个文件上传,ruby-on-rails,ruby,shrine,Ruby On Rails,Ruby,Shrine,我试图实现多个文件上传使用多态关联和神社 class Campaign < ApplicationRecord has_many :photos, as: :imageable, dependent: :destroy accepts_nested_attributes_for :photos, allow_destroy: true end class Photo < ApplicationRecord include ImageUploader::Attachmen

我试图实现多个文件上传使用多态关联和神社

class Campaign < ApplicationRecord
  has_many :photos, as: :imageable, dependent: :destroy
  accepts_nested_attributes_for :photos, allow_destroy: true
end

class Photo < ApplicationRecord
  include ImageUploader::Attachment.new(:image)
  belongs_to :imageable, polymorphic: true
end
class活动
浏览完文档后,我可以保存照片。
请告知如何在可成像范围内验证图像的唯一性。
我知道每个原始版本都可以生成签名,但这样做正确吗?

谢谢。

生成签名是判断两个文件是否具有相同内容的唯一方法,而无需将这两个文件都加载到内存中(您应该始终避免)。此外,如果将签名保存到列中,则使用签名意味着您可以使用数据库唯一性约束和/或ActiveRecord唯一性验证

这就是你如何使用神殿的方法:

# db/migrations/001_create_photos.rb
create_table :photos do |t|
  t.integer :imageable_id
  t.string  :imageable_type
  t.text    :image_data
  t.text    :image_signature
end
add_index :photos, :image_signature, unique: true

# app/uploaders/image_uploader.rb
class ImageUploader < Shrine
  plugin :signature
  plugin :add_metadata
  plugin :metadata_attributes :md5 => :signature

  add_metadata(:md5) { |io| calculate_signature(io) }
end

# app/models/image.rb
class Photo < ApplicationRecord
  include ImageUploader::Attachment.new(:image)
  belongs_to :imageable, polymorphic: true

  validates_uniqueness_of :image_signature
end
#db/migrations/001_create_photos.rb
创建表格:不需要照片|
t、 整数:可成像的\u id
t、 字符串:可成像_类型
t、 文本:图像数据
t、 文本:图像签名
结束
添加索引:照片,:图像\u签名,唯一:真
#app/uploaders/image_uploader.rb
类ImageUploader:签名
添加_元数据(:md5){| io |计算_签名(io)}
结束
#app/models/image.rb
班级照片<申请记录
包括ImageUploader::Attachment.new(:image)
属于:可成像,多态:真
验证图像签名的唯一性
结束

谢谢!在最后一天,我正在考虑使用jQuery进行直接上传