我正在使用carrierwave gem
来上传文件。我有一个包含一个nested_form field
的表单,试图从中上传多个文件。但是由于某些奇怪的原因,当我尝试上传多个文件时,multipart=true
无法正常工作。我收到以下错误消息:Validation failed: Attachments media files can't be blank
,在控制台中我收到了Unpermitted parameter: :media_files
....甚至认为提交表单时参数似乎已经过去了:
Started POST "/item/create" for 127.0.0.1 at 2018-10-23 13:08:19 +0300
Processing by ItemsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>
"tDpyPV9fPxLUHgRVb4G6P3eMLFUv0hKVnNPJCAcgYISCwjIbjA5bu/iXOy0k6yL
8+QWXs3MyoJ3lzzyhj3rqkw==", "item"=>{"title"=>"Sample", "description"
=>"Lorem", "attachments_attributes"=>{"0"=>{"media_files"=>[#
<ActionDispatch::Http::UploadedFile:0x007fe818a5ecb0 @tempfile=#
<Tempfile:/var/folders/hq/pr4rt14n7s31v3f6292wtjm00000gn/T/
RackMultipart20181023-1573-1r65sf9.jpg>, @original_filename=
"image1.jpg", @content_type="image/jpeg", @headers="Content-
Disposition: form-data; name=\"item[attachments_attributes]
[0][media_files][]\"; filename=\"image1.jpg\"\r\nContent-Type:
image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile
:0x007fe818a5ebe8 @tempfile=#
<Tempfile:/var/folders/hq/pr4rt14n7s31v3f6292wtjm00000gn
/T/RackMultipart20181023-1573-1lbr709.jpg>,
@original_filename="image2.jpg", @content_type=
"image/jpeg", @headers="Content-Disposition: form-data;
name=\"item[attachments_attributes][0][media_files][]\";
filename=\"image2.jpg\"\r\nContent-Type: image/jpeg\r\n">], "item_id"=>"#
<Item:0x007fe813ef65e8>"}}}, "commit"=>"Create"}
Unpermitted parameter: :media_files
这是我拥有的nested_form设置:
create_table "attachments", force: :cascade do |t|
t.integer "item_id"
t.string "media_files"
t.string "content_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
class Item < ApplicationRecord
has_many :attachments, dependent: :destroy
accepts_nested_attributes_for :attachments, allow_destroy: true
end
class Attachment < ApplicationRecord
belongs_to :item
mount_uploader :media_files, AttachmentUploader
validates_presence_of :media_files
end
这是attachments_controller.rb
方法:
def create
params["item"]["attachments_attributes"]["0"]["media_files"].each { |file| @attachment = Attachment.new(:media_files => file)}
respond_to do |format|
if @attachment.save
format.html { redirect_back fallback_location: root_path, notice: 'Attachment was successfully created.' }
format.json { render :'shows/show', status: :created, location: @attachment }
else
format.html { render :new }
format.json { render json: @attachment.errors, status: :unprocessable_entity }
end
end
end
和items_controller.rb
内的嵌套参数:
params.require(:item).permit(:title, :description, attachments_attributes: [:media_files, :item_id, :content_type])
具有嵌套字段的表单:
<%= form_for(@item, url: items_create_path, :html => { :multipart => true }) do |form| %>
<%= form.text_field :title, id: :item_title, autofocus: true, :class=>"form-control" %>
<%= form.text_area :description, id: :item_description, :class=>"form-control" %>
<%= form.fields_for :attachments do |f| %>
<div class="form-group">
<%= f.file_field :media_files, multiple: true, :id=>"upload-photo" %>
<% end %>
<%= form.submit "Create", :class=>"btn btn-default" %>
<% end %>
关于如何进行这项工作的任何想法?
答案 0 :(得分:0)
不允许的参数::media_files
表示不允许在以下行中使用参数“ media_files”:
params["item"]["attachments_attributes"]["0"]["media_files"].each { |file| @attachment = Attachment.new(:media_files => file)}
此行本身很奇怪,因为您要遍历参数,以便每次oO都实例化一个新的附件
这行几乎可以,只需更改attachments_attributes
:
params.require(:item).permit(:title, :description, attachments_attributes: [:item_id, :content_type, :media_files => []])
但这就是您必须赋予Item.new
的原因,因为您的表单指向items_create_path
。
您为什么希望它能到达attachments_controller.rb?