我正在尝试使用载波上传多个文件。每当我尝试上传文件时,我都可以上传唯一的单个文件。甚至我在floorplan控制器下添加了permit({image:[]})中的hash数组。
是控制器。
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
端
这是我上传文件的部分表格。
= 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"
之后,我附加了jquery文件上传器,如RailsCast第381集中所述
floorplan.coffee
jQuery ->
$('#new_floorplan').fileupload()
这些是我的数据库迁移
class AddImageToFloorplans < ActiveRecord::Migration[5.1]
def change
add_column :floorplans, :image, :string
end
end
答案 0 :(得分:0)
1- 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- new
行动
def new
@project = Peoject.new
@project.@project.floorplans.build
end
4.-以视图形式
<%= 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 %>
{p> 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-更多reference您可以按照
进行操作