回形针上不允许的参数 - Rails 5
Unpermitted parameters on paperclip - Rails 5
我试图保存上传的图片,但我的日志发送给我:
Unpermitted parameter: 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
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
检查您的日志:
"design_files"=>[#<ActionDispatch::...>]
design_files
是一个数组(因为 multiple
选项设置为 true
)。你应该允许一个数组:
def cover_params
params.require(:cover).permit(design_files: [])
end
How to permit an array?
我试图保存上传的图片,但我的日志发送给我:
Unpermitted parameter: 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
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
检查您的日志:
"design_files"=>[#<ActionDispatch::...>]
design_files
是一个数组(因为 multiple
选项设置为 true
)。你应该允许一个数组:
def cover_params
params.require(:cover).permit(design_files: [])
end
How to permit an array?