简体   繁体   中英

CarrierWave Multiple file Upload

I am trying to upload multiple files using carrier wave. Whenever I am trying to upload the file, I am able to upload the only single file. Even I have added the array of hash in permit({image: []}) under the floorplan controller.

It is the controller.

class FloorplansController < ApplicationController

def create
    @project = Project.find(params[:project_id])
    @floorplan  = Floorplan.create(params[:floorplan].permit(:name,{image: []}))
    @floorplan.user_id = current_user.id
    @floorplan.project_id = @project.id

    if @floorplan.save
        redirect_to project_path(@project)
    else
        render 'new'
    end
end

end

It is the form partial from where I am uploading the files.

= simple_form_for([@project, @project.floorplans.build],input_html: {multiple: true }) do |f|
= f.input :name, label: "Name the Floorplan"
= f.input :image, input_html: {multiple: true }, name:"floorplan[image][]", label: "Attach the Floorplan Image"
= f.button :submit, class: "button"

After that, I attached jquery file uploader as explained in RailsCast episode 381 floorplan.coffee jQuery -> $('#new_floorplan').fileupload()

And these are my db migrations

class AddImageToFloorplans < ActiveRecord::Migration[5.1]
   def change
     add_column :floorplans, :image, :string
   end
end

1- in Gemfile

gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'nested_form_fields'

2- application.js.erb

//= require jquery
//= require jquery_ujs
//= require nested_form_fields
//= require jquery-ui
//= require turbolinks
//= require_tree .

3- in new action

def new
  @project = Peoject.new
  @project.@project.floorplans.build
end

4.- in view form

<%= form_for @project do |f| %>
    <%= f.text_field :name, class: 'form-control' %>
   <%=f.nested_fields_for :floorplans do |floorplan|%>
     <%=floorplan.remove_nested_fields_link 'Remove'%>
     <%=floorplan.file_field :image%>
   <%end%>
   <%=f.add_nested_fields_link :floorplans, 'Add Photo'%>
  <%= f.submit 'Submit', class: 'btn btn-primary' %>
<% end %>

5- in create action

def create
    @project = Project.new(project_params)
    if @floorplan.save
        redirect_to project_path(@project)
    else
        render 'new'
    end
end

private
def project_params
  params.require(:project).permit(:name, floorplans_attributes: [:image])
end

6- for more reference you can follow this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM