我在以下表单中包含此字段:<%= form.file_field :image, multiple: true, name: 'attachment[image][]', id: "uploads" %>
<%= form_for @attachment, url: create_attachment_path(@attachment), :html => {:id => "form", :multipart => true }, method: :post do |form| %>
<% if @attachment.errors.any? %>
<div class="centerList">
<div id="error_explanation">
<h2><%= pluralize(item.errors.count, "error") %> <%= t 'store_item_edit_4' %></h2>
<% @attachment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</div>
</div>
<% end %>
<div class="form-group">
<div class="text-center">
<label class="btn btn-primary"><%= t 'store_item_edit_5' %><span style="display:none;">
<%= hidden_field_tag :item_id, params[:item_id], value: @item.id %>
<%= form.file_field :image, multiple: true, name: 'attachment[image][]', id: "uploads" %></span></label>
<%= form.submit '', :style => "display: none;" %>
<% end %>
我尝试使用此操作向数据库添加多个图像:
def create
@attachment = Attachment.new(attachment_params)
respond_to do |format|
if @attachment.save
format.html { redirect_back fallback_location: root_path, notice: 'Image was successfully uploaded.' }
else
format.html { render :new }
format.json { render json: @attachment.errors, status: :unprocessable_entity }
end
end
end
这些是控制器内部的参数:
def attachment_params
params.require(:attachment).permit(:item_id, :account_id, :image)
end
但是我得到了`未经许可的参数:: image&#39;在控制台内部,db行被创建但是具有空值。
Started POST "/attachments/create" for 127.0.0.1 at 2018-03-15 11:50:26 +0200
Processing by AttachmentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"s0S6EwMgF9ac6djr028OfPBCXdKTM/NTjZFHdxi4Ot9MlUQZjHa5+0PNIu0Fg54b36SdWVzUE0g7fb3BJtF2gA==", "item_id"=>"2", "attachment"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007f82cf881090 @tempfile=#<Tempfile:/var/folders/hq/pr4rt14n7s31v3f6292wtjm00000gn/T/RackMultipart20180315-1190-mrrhdh.jpg>, @original_filename="image5.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachment[image][]\"; filename=\"image5.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f82cf880f78 @tempfile=#<Tempfile:/var/folders/hq/pr4rt14n7s31v3f6292wtjm00000gn/T/RackMultipart20180315-1190-18zuptn.jpg>, @original_filename="image6.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachment[image][]\"; filename=\"image6.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f82cf880ed8 @tempfile=#<Tempfile:/var/folders/hq/pr4rt14n7s31v3f6292wtjm00000gn/T/RackMultipart20180315-1190-1iuevmq.jpg>, @original_filename="image7.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachment[image][]\"; filename=\"image7.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f82cf880e88 @tempfile=#<Tempfile:/var/folders/hq/pr4rt14n7s31v3f6292wtjm00000gn/T/RackMultipart20180315-1190-ynpy5a.jpg>, @original_filename="image8.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachment[image][]\"; filename=\"image8.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}
Store Load (1.0ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 ORDER BY "stores"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Unpermitted parameter: :image
(0.2ms) BEGIN
SQL (0.7ms) INSERT INTO "attachments" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2018-03-15 09:50:26.308178"], ["updated_at", "2018-03-15 09:50:26.308178"]]
(0.7ms) COMMIT
答案 0 :(得分:0)
西蒙的评论是正确的。您需要允许一组标量类型。在你的场景中一个ActionDispatch :: Http :: UploadedFile对象,它被接受为标量。
http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters
IMO如果属性相同,则无需在field_form上设置名称:name: 'attachment[image][]'
,当您使用multiple: true
时,name
属性应该设置为完全相同。
还要告知您正在使用哪个rails版本。