【问题标题】:Paperclip Unpermitted parameter: image回形针 未经许可的参数:图像
【发布时间】:2017-07-13 09:49:54
【问题描述】:

我正在尝试让 Paperclip 在表单提交时将图像从我的节日模型上传到 s3,但我收到 Unpermitted parameter: image. 错误

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

我认为我已将问题缩小到我对数据库的发布请求无法处理分配给festival.image 的文件对象,但无法弄清楚我将如何在发布请求中表示它。

我在前端使用 react on rails 以 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

节日对象打印到控制台 通过 axios 向数据库发布请求

 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

【问题讨论】:

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


    【解决方案1】:

    由于您请求中的image 参数是一个哈希"image"=&gt;{"preview"=&gt;"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
    

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

    【讨论】:

    • 谢谢你,摆脱了未经许可的参数错误!现在我得到 {"preview"=>"blob:localhost:5000/76b95cb5-45bf-46a9-ba7b-f5b9ad127521"} 的 Paperclip No Handler Error 但从其他堆栈溢出问题将是我没有将正确的类传递给 Paperclip 所以当我得到回家!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多