2

我想使用Carrierwave和Dropzone.js上傳多個圖像,但是當我提交表單時出現以下錯誤。Carrierwave,Dropzone.js Rails 4,強大的參數

請注意,沒有dropzone.js的表單工作正常,這意味着當我只是使用HTML5上傳多個圖像。

請參考下面當I(使用懸浮窗通過AJAX)提交表單其中創建哈希


「project_images_attributes」=> [{ 「圖像」=

[#,@ original_filename =「artist4.jpg」,@ content_type =「image/jpeg」,@ headers =「Content-Disposition:form-data; name = \」project [project_ images_attributes] [] [image] [] \「; filename = \「artist4.jpg \」\ r \ nContent-Type:image/jpeg \ r \ n「>,#,@ original_filename =」feed1.jpg「,@con tent_type =」image/jpeg「, @ headers =「Content-Disposition:form-data;名= \ 「項目[project_images_attributes] [] [圖像] [] \」; filename = \「feed1.jpg \」\ r \ nContent-Type:image/jpeg \ r \ n「>,#,@ original_filename =」gallery1.jpg「,@ content_type =」image/jpeg「,@ headers = 「Content-Disposition: form-data;名= \ 「項目[project_images_attributes] [] [圖像] [] \」; filename = \「gallery1.jpg \」\ r \ nContent-Type:image/jpeg \ r \ n「>]}]},」project-vide o「=> [」「],」commit「=>創建項目「}

不允許的參數:圖像

請注意我已經列入白名單在我的控制器的圖像PARAMS也它的工作使用HTML 5,但不使用Ajax和dropzone.js

有沒有人知道如何解決這個問題?

謝謝

+0

從您將屬性列入白名單的控制器共享相關代碼。 –

回答

0

代碼的背景情況,我結束了:

private 
    def pin_image_params 
    #params.require(:pin_image).permit(:photo) (not permitted) 
    #params.require(:pin_image).permit(photo: [:'0']) (permitted but action handler error) 
    #params.require(:pin_image)["photo"].values.first (stringify_keys error) 
    params.require(:photos).values 
    end 

強PARAMS保持返回未經許可PARAMS:照片。看着控制檯,我看到參數正在編號,我可以訪問它們的唯一方法是按數字跳躍。儘管如此,試圖直接將它們傳遞給create返回錯誤。這是因爲模型無法理解編號散列的格式。它不夠聰明,無法知道它需要遍歷每個附加的圖像。所以:

def create 
    # so pin_image_params is an array of files basically 
    @pin_images = pin_image_params.inject([]) do |memo, photo| 
    # this pattern will store the memo into the @pin_images reference 
     memo << PinImage.create(photo: photo) 
    # keying it explicitly 
    end 
    if @pin_images.present? 
     render json: { id: @pin_images.map(&:id) }, :status => 200 
    else 
     # you need to send an error header, otherwise Dropzone 
     # will not interpret the response as an error: 
     render json: { error: @pin_images.errors.full_messages.join(',')}, :status => 400 
    end 
end 

我控制器返回ID的列表,這些都在一個隱藏字段捕獲擁有PinImage(PIN)的模型。 (這是通過在ajax上通過dropzone提交PinImages的成功回調完成的。)所以當提交主模型時,我也必須處理在那裏的關聯,但是我發現我更喜歡通過nested_attributes_for來混淆。因人而異。

0

看看你提交表單的方式。它是form.submit嗎?一旦上傳完成,Dropzone會自動通過ajax提交文件