[英]Rails form field array name and params
I have this field inside the following form: <%= form.file_field :image, multiple: true, name: 'attachment[image][]', id: "uploads" %>
我在以下表单中包含此字段: <%= 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 %>
I'm trying to add multiple images to the db with this action: 我正在尝试通过此操作将多个图像添加到数据库:
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
these are the params I have inside the controller: 这些是我在控制器内部拥有的参数:
def attachment_params
params.require(:attachment).permit(:item_id, :account_id, :image)
end
But I'm getting `Unpermitted parameter: :image' inside the console, the db row gets created but with null values. 但是我在控制台中得到了“ Unpermitted parameter::image”,创建了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
Simon's comment is correct. 西蒙的评论是正确的。 You need to permit an array of scalar types. 您需要允许一个标量类型数组。 In your scenario an ActionDispatch::Http::UploadedFile object, which is accepted as scalar. 在您的方案中,有一个ActionDispatch :: Http :: UploadedFile对象,该对象被视为标量。
http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters
IMO there's no need to set the name on field_form if the attribute is the same: name: 'attachment[image][]'
, when you using multiple: true
the name
property should be set exactly like that. 如果属性相同,则IMO无需在field_form上设置名称: name: 'attachment[image][]'
,当您使用multiple: true
, name
属性应设置为完全相同。
Also inform which rails version you're using. 同时告知您所使用的Rails版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.