2017-04-19 93 views
0

我一直在努力解決這個問題。我試圖用Dropclip.js上傳多個「照片」模型和paperclip屬性「附件」。目前我正在使用DropZone-Rails gem(我試過幾個版本都有相同的結果)。Dropzone JS在Rails 4中未經允許的參數

# 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.autoDiscover = FALSE;

在我的模板:

$("#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(); 

我和懸浮窗的後備形式:

<%= 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 %> 

我的日誌對PARAMS輸出(前得到過濾):

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

任何幫助將不勝感激。我搜索了一切,並嘗試了一切,似乎無法使它工作。謝謝。

回答

1

從您的型號Photo定義中,顯然您要上傳每個記錄1個附件。但是您在控制器中收到的參數是用於上傳多個附件的參數。

{ ..., attachment => { 0 => #<ActionDispatch::Http::UploadedFile }, ... } 

如果你想允許在您的PARAMS只有1個附件,您的PARAMS應該是形式:

{ ..., attachment => #<ActionDispatch::Http::UploadedFile, ... } 

dropzone options看,我會說,嘗試在下降改變uploadMultiple: trueuploadMultiple: false區域配置。讓我知道,如果問題仍然存在。

+0

你是對的!我在這方面花了太多時間。謝謝! – QNeville