【问题标题】:Multiple upload images in active_admin with Active Storage使用 Active Storage 在 active_admin 中上传多个图像
【发布时间】:2018-11-14 03:56:57
【问题描述】:

我在 activeadmin 中找到了关于使用 Active Storage 上传图片的有用文章:https://medium.com/@maris.cilitis/using-ruby-on-rails-active-storage-image-uploads-for-active-admin-backed-resources-5638a9ca0b46

但是如何在activeadmin中以同样的方式上传多张图片和Active Storage呢?

【问题讨论】:

    标签: activeadmin rails-activestorage ruby-on-rails-5.2


    【解决方案1】:

    你只需要做一些改变

    型号:

    has_many_attached :images
    

    而不是

    has_one_attached :image
    

    活跃管理员:

    permit_params images: []
    
    form do |f|
      f.inputs do
        f.input :images, as: :file, input_html: { multiple: true }
      end
    end
    

    你可以选择很多文件上传

    【讨论】:

    • 在我删除此行 input_html: { multiple: true } 并添加 form :html => { :multipart => true } do |f| 后它可以工作
    • 这个问题是我们没有选择将所选图像保留在编辑页面上。每次我们进入编辑页面时,我们都需要选择所有图像。有没有办法在这里传递旧的选定图像
    【解决方案2】:

    这对我有用,使用活动存储在 active_admin 中上传和显示多张图片。

    ActiveAdmin.register Post do
    
      permit_params :content, :published, :user_id, :images => []
    
      form html: { multipart: true } do |f|
        f.inputs "Publication" do
          f.input :user
          f.input :content
          f.input :published
          f.input :images, as: :file, input_html: { multiple: true }
        end
    
        f.actions
      end
    
      show do
        attributes_table do
          row :images do
            div do
              post.images.each do |img|
                div do
                  image_tag url_for(img), size: "200x200"
                end
              end
            end
          end
    
          row :content
          row :published
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-15
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 2020-12-01
      • 2023-04-09
      相关资源
      最近更新 更多