单个模型的载波多个上传器 [英] carrierwave multiple uploader for single model

查看:74
本文介绍了单个模型的载波多个上传器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在本教程 https:// kolosek的帮助下创建了一个图库模块。 com / carrierwave-upload-multiple-images / ,现在我想向同一模型添加一个具有主图像的图像属性。因此,我想再创建一个上传器,但是对于如何称呼控制器可以有人帮我感到有些困惑?

I have created a gallery module with the help of this tutorial https://kolosek.com/carrierwave-upload-multiple-images/ and now I want to add one more image attribute has a master image to the same model. So I thot of creating one more uploader but I'm bit confused about how to call that the controller can anybody help me out?

我的代码如下所示:

型号代码:

class Image < ApplicationRecord
  belongs_to :gallery 
  mount_uploader :image, ImageUploader
  mount_uploader :avatar, AvatarUploader
end

class Gallery < ApplicationRecord
 has_many :images
 accepts_nested_attributes_for :images
end

gallery_controller .rb

gallery_controller.rb

 class GalleriesController < AdminController   
  def index
    @galleries = Gallery.all
  end

  def show
   @images = @gallery.images.all
  end

  def new
   @gallery = Gallery.new
   @image = @gallery.images.build
  end 

  def create
   @gallery = Gallery.new(gallery_params)
   respond_to do |format|
   if @gallery.save
     params[:images]['image'].each do |a|
      @images = @gallery.images.create!(:image => a, :gallery_id =>  @gallery.id)
    end
     format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
     format.json { render :show, status: :created, location: @gallery }
   else
     format.html { render :new }
     format.json { render json: @gallery.errors, status: :unprocessable_entity }
   end
 end
end

def gallery_params
  params.require(:gallery).permit(:title, :details, :status, images_attributes:[:id, :gallery_id, :image, :avatar])
end   

图像控制器代码:

class ImagesController < AdminController
 def image_params
   params.require(:image).permit(:gallery_id, :slideshow_id,:image, :avatar)
 end
end

form.html.erb

form.html.erb

<%= form.fields_for :images do |p| %>
 <div class="field">
   <%= form.label :master_image, class: "col-2 col-form-label" %>
  <%= form.file_field :avatar, :multiple => true, name: "images[avatar][]" %>
  </div>
<% end %>
<%= form.fields_for :images do |p| %>
  <div class="field">
    <%= form.label :image, class: "col-2 col-form-label" %>
    <%= form.file_field :image, :multiple => true, name: "images[image][]" %>
  </div>
<% end %>

我对修改Gallery Controller创建部件感到困惑。

I am confused in modifing the gallery controller create part.

推荐答案

您可以在 create 方法中将图像创建添加两次。如果您有2个上传者

You can add the images creation 2 times in the create method if you have 2 uploaders

params[:images]['image'].each do |a|
  @gallery.images.create!(:image => a, :gallery_id =>  @gallery.id)
end
params[:images]['avatar'].each do |a|
  @gallery.images.create!(:avatar => a, :gallery_id =>  @gallery.id)
end

但我认为这有点冗长,并且如果您不想以表单形式上传任何图像,它并不能真正阻止(返回 params [: images] ['...'] nil)

but i think it's a little bit verbose and it not really prevents if you don't want to upload any image in your form (return params[:images]['...'] nil)

顺便说一句,您的表单对于 form是不正确的。 。是:

Btw your form is not correct for the form.fields_for. It's :

<%= form.fields_for :images_attributes do |p| %>
 <div class="field">
   <%= p.label :master_image, class: "col-2 col-form-label" %>
   <%= p.file_field :avatar, :multiple => true, name: "images[avatar][]" %>
  </div>
<% end %>
<%= form.fields_for :images_attributes do |p| %>
  <div class="field">
    <%= p.label :image, class: "col-2 col-form-label" %>
    <%= p.file_field :image, :multiple => true, name: "images[image][]" %>
  </div>
<% end %>

这篇关于单个模型的载波多个上传器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆