2017-02-23 99 views
1

我想要回形針將圖片上傳到s3從表單提交我的節日模型,但我收到未經許可的參數:圖像。錯誤回形針未經允許的參數:圖片

我檢查了強烈的參數,模型內容驗證並無法通過回形針文檔閱讀。

我想我縮小了問題的範圍,直到我的發佈請求數據庫無法處理被分配給festival.image的File對象,但無法弄清楚我將如何在發佈請求中表示這個問題。

我正在捕獲軌道中的數據,在前端的導軌上使用反應,Rails作爲後端。我跟着這個示例代碼https://github.com/carlbaron/react-file-upload-demo

我也使用React-dropzone捕獲上傳的文件,它添加了圖像預覽的預覽屬性。

一直呆在這一段時間了,任何幫助非常感謝!

初發布請求印刷的安慰

Processing by FestivalsController#create as JSON 

Parameters: {"festival"=>{"fest_name"=>"Test Festival", "image"=>{"preview"=>"blob:http://localhost:5000/76b95cb5-45bf-46a9-ba7b-f5b9ad127521"}}} 

| Unpermitted parameter: image 

經由Axios公司打印到控制檯 festival object POST請求發送到DB 節對象

postFestival(festival) { 
    let config = { 
     responseType: 'json', 
     processData: false, 
     contentType: false, 
     headers: ReactOnRails.authenticityHeaders(), 
    }; 
     let str = JSON.stringify(festival); 
     console.log("ENTITY IS " + str); 

     //returns 
     //ENTITY IS {"fest_name":"Test Festival","image":{"preview":"blob:http://localhost:5000/76b95cb5-45bf-46a9-ba7b-f5b9ad127521"}} 

     return(
     request.post('/festivals/create', {festival}, config) 
    ); 
    }, 

節.rb

class Festival < ApplicationRecord 

    has_attached_file :image, default_url: "/assets/ASOT-COVER.png" 
    validates_attachment :image, 
         content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] } 


    end 

節日控制器

def create 

    @festival = Festival.create(festival_params) 

    puts "festival.image =" + @festival.image.inspect 
    #returns = festival.image =#<Paperclip::Attachment:0x007fc288868bf0 @name=:image, @name_string="image", @instance=# 

    if @festival.save 
     puts "Festival SAved = + " + @festival.inspect 
     #returns the festival object saved to the DB minus the image param 
    else 
     respond_to do |format| 
     format.json { render json: @festival.errors, status: :unprocessable_entity} 
     puts "ERROR = " + @festival.errors.inspect 
     end 
    end 

    private 

    def festival_params 

     params.require(:festival).permit(:fest_name, :fest_organizer, :fest_location, 
             :fest_date, :fest_url, :fest_venue, :fest_description, 
            :image) 
    end 
    end 

回答

1

正如您的要求image參數是一個哈希"image"=>{"preview"=>"blob:http://localhost:5000/76b95cb5-45bf-46a9-ba7b-f5b9ad127521"},你將需要修改festival_params方法是這樣的:

def festival_params 
    params.require(:festival).permit(:fest_name, :fest_organizer, :fest_location, 
            :fest_date, :fest_url, :fest_venue, :fest_description, 
           { image: :preview }) 
end 

設我知道它是否解決了錯誤。

+0

謝謝,這擺脫了未經許可的參數錯誤!現在我得到了Paperclip No Handler Error for {「preview」=>「blob:http:// localhost:5000/76b95cb5-45bf-46a9-ba7b-f5b9ad127521」}但是來自其他堆棧溢出問題將Im沒有傳遞正確班到回形針,所以我會試圖在我回家時看看這個! – monopolyman