回形针上的未允许参数 - Rails 5

时间:2017-03-15 10:12:12

标签: ruby-on-rails ruby paperclip

我尝试保存上传的图片但我的日志却发送给我:

  
    

未经许可的参数:design_files

  

然而,这是我的控制器发回给我的哈希参数:

"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">]

因此params design_files确实存在,但结果是一个数组,可能是那里的问题。

我让你看到我的其余代码:

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

覆盖/ _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

1 个答案:

答案 0 :(得分:0)

检查您的日志:

->*

"design_files"=>[#<ActionDispatch::...>] 是一个数组(因为design_files选项设置为multiple)。你应该允许一个数组:

true

How to permit an array?