0

I tried to save uploaded images but my log send me :

Unpermitted parameter: design_files

However this is the hash parameters my controller is sending back to me :

"design_files"=>[#<ActionDispatch::Http::UploadedFile:0x007fce350cbcb8 @tempfile=#<Tempfile:/var/folders/0w/pznf2h6j0q9gqcbcthg1kgm40000gn/T/RackMultipart20170315-5734-26bwb.png>, @original_filename="ebf84ecaf15f684b53f18b9313cd1325.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"cover[design_files][]\"; filename=\"ebf84ecaf15f684b53f18b9313cd1325.png\"\r\nContent-Type: image/png\r\n">]

So the params design_files does exist, but the results is an array, may be the problem come from there.

I let you see the rest of my code :

covers_controller.rb

class CoversController < ApplicationController
  before_action :set_cover, only: [:show, :edit, :update, :destroy]

  def new
    @book = set_book
    @cover = @book.build_cover
  end

  def create
    @book = set_book
    @cover = @book.build_cover(cover_params)
    if @cover.save!
      redirect_to new_command_path(commandable_id: @cover, commandable_type: @cover.class)
    else
      render :new
    end
  end

  ....

   private
  ...

  def cover_params
    params.require(:cover).permit(:design_files) 
                                   # according to other solutions find on stack I tried : design_files_attributes: {:design_files}, also tried {design_files: :preview}                                
  end
end

cover.rb

class Cover < ApplicationRecord
  validates_presence_of :design_files
  has_attached_file :design_files
  do_not_validate_attachment_file_type :design_files
end

covers/_form

= form_for cover, html: { multipart: true } do |f|
  - if cover.errors.any?
    #error_explanation
      h2
        = pluralize(cover.errors.count, "error")
        |  prohibited this cover from being saved:
      ul
        - cover.errors.full_messages.each do |message|
          li
            = message

  = f.label :design_files
  = f.file_field :design_files, multiple: true

  .actions
    = f.submit
Orsay
  • 1,080
  • 2
  • 12
  • 32

1 Answers1

0

Check your logs:

"design_files"=>[#<ActionDispatch::...>]

design_files is an array(because multiple option is set to true). You should permit an array:

 def cover_params
   params.require(:cover).permit(design_files: [])                                                       
 end

How to permit an array?

Community
  • 1
  • 1
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
  • Thank you for your answer. I tried it, see my main post edit – Orsay Mar 15 '17 at 10:47
  • @Orsay that's an another question, is it? Stackoverflow isn't your debugging tool, please try to understand the _problem_, google is your friend. Happy coding! – Roman Kiselenko Mar 15 '17 at 10:54