1

I have this field inside the following form: <%= 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.

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
3
  • 1
    try params.require(:attachment).permit(:item_id, :account_id, image: []) Commented Mar 15, 2018 at 10:15
  • Thanks for the reply @Simon Franzen. unfortunately it returns an error: param is missing or the value is empty: attachment Commented Mar 15, 2018 at 10:18
  • You are setting the attachments as a list the wrong way. Look here stackoverflow.com/questions/34110469/… Commented Mar 15, 2018 at 10:32

1 Answer 1

1

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.

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.

Also inform which rails version you're using.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.