2017-02-23 97 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