Rails 4 中的 Dropzone JS 不允许的参数
Dropzone JS Unpermitted Parameters In Rails 4
我整晚都在为这个问题苦苦挣扎。我正在尝试上传多个 "Photo" 模型 Dropzone.js 和回形针属性 "attachment"。目前我正在使用 DropZone-Rails gem(我尝试了几个版本,结果都一样)。
# photo.rb
class Photo < ActiveRecord::Base
has_attached_file :attachment, styles: { medium: "720x720>", thumb: "360x360>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/
belongs_to :gallery
belongs_to :user
end
如下所示,我已将附件参数列入白名单。
#photos_controller.rb
class PhotosController < ApplicationController
before_action :set_photo, only: [:show, :edit, :update, :destroy]
.........
private
# Use callbacks to share common setup or constraints between actions.
def set_photo
@photo = Photo.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def photo_params
params.require(:photo).permit(:attachment, :gallery_id)
end
DropZone 按预期加载,此处配置为:
Dropzone.autoDiscover = 假;
在我的模板中:
$("#new_photo").dropzone({
// restrict photo size to a maximum 1MB
maxFilesize: 4,
paramName: "photo[attachment]",
addRemoveLinks: true,
uploadMultiple: true,
success: function(file, response){
$(file.previewTemplate).find('.dz-remove').attr('id', response.fileID);
$(file.previewElement).addClass("dz-success");
}
});
var myDropzone = new Dropzone();
我使用 DropZone 后备的表单:
<%= form_for(Photo.new, html: { multipart: true, class: "dropzone"}) do |f| %>
<%= f.hidden_field :gallery_id, :value=>@gallery.id %>
<div class="fallback">
<%= f.file_field :attachment %>
<%= f.submit "Upload" %>
</div>
<% end %>
我的日志的参数输出是(过滤前):
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5ftDcdfB1YpL4UF1fupC/I11xRx3HV5GYih5kshO/KSXzSrZS5Iu1wVRhbLFiyfQWQbwP3R5mxeoJLiM+cUJDg==", "photo"=>{"gallery_id"=>"13", "attachment"=>{"0"=>#<ActionDispatch::Http::UploadedFile:0x007f8058e13c48 @tempfile=#<Tempfile:/var/folders/jb/f02hpkhs3813_b6w_bvyxkxw0000gn/T/RackMultipart20170419-16050-8s5zd8.jpg>, @original_filename="624bbbf5b3c8a293950c8ed24a0c4eef.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[attachment][0]\"; filename=\"624bbbf5b3c8a293950c8ed24a0c4eef.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}
这导致:
Unpermitted parameter: attachment
{"gallery_id"=>"13"}
如有任何帮助,我们将不胜感激。我已经搜索了所有内容并尝试了所有内容,但似乎无法正常工作。谢谢。
根据您的模型 Photo
定义,很明显您希望每条记录上传 1 个附件文件。但是您在控制器中收到的参数是用于上传多个附件的参数。
{ ..., attachment => { 0 => #<ActionDispatch::Http::UploadedFile }, ... }
如果您只想在您的参数中允许 1 个附件,您的参数应采用以下格式:
{ ..., attachment => #<ActionDispatch::Http::UploadedFile, ... }
看着 dropzone options,我会说,尝试在拖放区配置中将 uploadMultiple: true
更改为 uploadMultiple: false
。如果问题仍然存在,请告诉我。
我整晚都在为这个问题苦苦挣扎。我正在尝试上传多个 "Photo" 模型 Dropzone.js 和回形针属性 "attachment"。目前我正在使用 DropZone-Rails gem(我尝试了几个版本,结果都一样)。
# photo.rb
class Photo < ActiveRecord::Base
has_attached_file :attachment, styles: { medium: "720x720>", thumb: "360x360>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/
belongs_to :gallery
belongs_to :user
end
如下所示,我已将附件参数列入白名单。
#photos_controller.rb
class PhotosController < ApplicationController
before_action :set_photo, only: [:show, :edit, :update, :destroy]
.........
private
# Use callbacks to share common setup or constraints between actions.
def set_photo
@photo = Photo.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def photo_params
params.require(:photo).permit(:attachment, :gallery_id)
end
DropZone 按预期加载,此处配置为: Dropzone.autoDiscover = 假;
在我的模板中:
$("#new_photo").dropzone({
// restrict photo size to a maximum 1MB
maxFilesize: 4,
paramName: "photo[attachment]",
addRemoveLinks: true,
uploadMultiple: true,
success: function(file, response){
$(file.previewTemplate).find('.dz-remove').attr('id', response.fileID);
$(file.previewElement).addClass("dz-success");
}
});
var myDropzone = new Dropzone();
我使用 DropZone 后备的表单:
<%= form_for(Photo.new, html: { multipart: true, class: "dropzone"}) do |f| %>
<%= f.hidden_field :gallery_id, :value=>@gallery.id %>
<div class="fallback">
<%= f.file_field :attachment %>
<%= f.submit "Upload" %>
</div>
<% end %>
我的日志的参数输出是(过滤前):
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5ftDcdfB1YpL4UF1fupC/I11xRx3HV5GYih5kshO/KSXzSrZS5Iu1wVRhbLFiyfQWQbwP3R5mxeoJLiM+cUJDg==", "photo"=>{"gallery_id"=>"13", "attachment"=>{"0"=>#<ActionDispatch::Http::UploadedFile:0x007f8058e13c48 @tempfile=#<Tempfile:/var/folders/jb/f02hpkhs3813_b6w_bvyxkxw0000gn/T/RackMultipart20170419-16050-8s5zd8.jpg>, @original_filename="624bbbf5b3c8a293950c8ed24a0c4eef.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[attachment][0]\"; filename=\"624bbbf5b3c8a293950c8ed24a0c4eef.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}
这导致:
Unpermitted parameter: attachment
{"gallery_id"=>"13"}
如有任何帮助,我们将不胜感激。我已经搜索了所有内容并尝试了所有内容,但似乎无法正常工作。谢谢。
根据您的模型 Photo
定义,很明显您希望每条记录上传 1 个附件文件。但是您在控制器中收到的参数是用于上传多个附件的参数。
{ ..., attachment => { 0 => #<ActionDispatch::Http::UploadedFile }, ... }
如果您只想在您的参数中允许 1 个附件,您的参数应采用以下格式:
{ ..., attachment => #<ActionDispatch::Http::UploadedFile, ... }
看着 dropzone options,我会说,尝试在拖放区配置中将 uploadMultiple: true
更改为 uploadMultiple: false
。如果问题仍然存在,请告诉我。