Paperclip未经许可的参数:图像

时间:2017-02-23 04:09:03

标签: ruby-on-rails ruby reactjs paperclip react-on-rails

我正在尝试让Paperclip将图像从表单提交上的节日模型上传到s3但收到未经许可的参数:图像。错误

我检查了强大的参数,模型内容验证并通过回形针文件阅读无济于事。

我认为我已经将问题缩小到我的发布请求,DB无法处理分配给festival.image的File对象,但无法弄清楚我将如何在post请求中表示这一点。 / p>

我正在使用Rails作为后端的前端轨道上的反应来捕获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

节目对象打印到控制台 festival object 通过axios向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)
      );
     },

Festival.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 个答案:

答案 0 :(得分: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

如果它能解决错误,请告诉我。