我尝试使用carrierwave在active_admin中上传多个图像。所以根据carrierwave文档获取一个数组字段,并允许嵌套字段,但上传多个图像后我收到错误。
error:
AdminUser Load (0.7ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.3ms) BEGIN
SQL (0.8ms) INSERT INTO "albums" ("title", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["title", "dvdfbfdbfdb"], ["created_at", 2016-08-10 18:36:56 UTC], ["updated_at", 2016-08-10 18:36:56 UTC]]
SQL (0.9ms) INSERT INTO "images" ("created_at", "updated_at", "avatars", "avatarable_id", "avatarable_type") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", 2016-08-10 18:36:56 UTC], ["updated_at", 2016-08-10 18:36:56 UTC], ["avatars", "{kfos6fofbk1pbhrw2a5x.png,meaxlow3fgehcoepklm7.jpg}"], ["avatarable_id", 22], ["avatarable_type", "Album"]]
SQL (1.8ms) UPDATE "images" SET "avatars" = 'image/upload/v1470854220/kfos6fofbk1pbhrw2a5x.png' WHERE "images"."id" = $1 [["id", 23]]
(0.3ms) ROLLBACK
Completed 500 Internal Server Error in 4632ms (ActiveRecord: 4.7ms)
ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: malformed array literal: "image/upload/v1470854220/kfos6fofbk1pbhrw2a5x.png"
LINE 1: UPDATE "images" SET "avatars" = 'image/upload/v1470854220/kf...
^
DETAIL: Array value must start with "{" or dimension information.
: UPDATE "images" SET "avatars" = 'image/upload/v1470854220/kfos6fofbk1pbhrw2a5x.png' WHERE "images"."id" = $1):
activerecord (5.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'
activerecord (5.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_no_cache'
activerecord (5.0.0) lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
activesupport (5.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activerecord (5.0.0) lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
activerecord (5.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `exec_no_cache'
activerecord (5.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:587:in `execute_and_clear'
activerecord (5.0.0) lib/active_record/connection_adapters/postgresql/database_statements.rb:116:in `exec_delete'
activerecord (5.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:133:in `update'
activerecord (5.0.0) lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `update'
ActiveAdmin.register Album do
permit_params :title,images_attributes: [:id,:_destroy,:avatars=>[]]
form html: { multipart: true } do |f|
f.inputs do
f.input :title
f.has_many :images, allow_destroy: true do |a|
a.input :avatars, :as => :file ,input_html:{multiple: true}
end
end
f.actions
end
end
class Album < ApplicationRecord
has_many :images, as: :avatarable, dependent: :destroy
accepts_nested_attributes_for :images, :allow_destroy => true
end
class Image < ApplicationRecord
belongs_to :avatarable, polymorphic: true
mount_uploaders :avatars, AvatarUploader
end
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
def extension_white_list
%w(jpg jpeg gif png)
end
end
答案 0 :(得分:0)
您可以尝试使用其他方法来完成多次上传。 尝试以示例的方式查看此示例项目: https://github.com/taragano/Cloudinary_multiple_uploads