【问题标题】:Active admin multiple file/image upload with paperclip活动管理员使用回形针上传多个文件/图像
【发布时间】:2023-03-31 20:34:01
【问题描述】:

我使用 Active admin,我需要上传包含大量图片的画廊。我该怎么做? 我的代码:

class Gallery < ActiveRecord::Base
  belongs_to :event
  has_many :images

  attr_accessible :name, :publish, :images, :image, :images_attributes
  accepts_nested_attributes_for :images, allow_destroy: true

  validates :name, presence: true

end

class Image < ActiveRecord::Base
  belongs_to :gallery

  attr_accessible :url
  has_attached_file :url, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end


ActiveAdmin.register Gallery do
    form html: { multipart: true }  do |f|
          f.inputs  do
            f.input :name
            f.input :images, as: :file, input_html: { multiple: true}
          end            
          f.buttons
    end  
end

我有这个错误:

Image(#70319146544460) expected, got ActionDispatch::Http::UploadedFile(#70319105893880)

【问题讨论】:

  • 我也有同样的问题。问题解决了吗?

标签: ruby-on-rails paperclip activeadmin formtastic


【解决方案1】:

试试这个:

ActiveAdmin.register Gallery do
  form multipart: true do |f|
    f.inputs do
      f.input :name

      f.has_many :images do |p|
        p.input :url
      end
    end

    f.actions
  end
end

【讨论】:

  • 我收到undefined method new_record?对于 nil:NilClass`。似乎是 has_many 的错误。
  • 实际上,在模型中使用accepts_nested_attributes_for(由OP指出)为我修复了它。我的错!
  • 我们可以使用它一次性上传多个文件吗?
【解决方案2】:

好的,我设法解决了:

尝试执行以下操作:

ActiveAdmin.register Gallery do
  form html: { multipart: true }  do |f|
    f.inputs  do
      f.input :name
      file_field_tag("gallery_images_url", multiple: true, name: "gallery[gallery_images_attributes][][url]")
    end            
    f.buttons
  end  
end

我通过以下博客文章找到了该解决方案:http://www.tkalin.com/blog_posts/multiple-file-upload-with-rails-3-2-paperclip-html5-and-no-javascript

【讨论】:

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