编程语言
首页 > 编程语言> > javascript-带base64的回形针:#的未定义方法`stringify_keys’

javascript-带base64的回形针:#的未定义方法`stringify_keys’

作者:互联网

请帮助我…我使用回形针将canvas标记(base64)中的1张图片上传到aws-s3.

我的控制器

    def create
    decoded_file = Base64.decode64(params[:photo])
      begin
        file = Tempfile.new(['test', '.jpg']) 
        file.binmode
        file.write decoded_file
        file.close
        @photo.photo =  file
        if @photo.save
          render :json => {:message => "Successfully uploaded the profile picture."}
        else
          render :json => {:message => "Failed to upload image"}
        end
      ensure
        file.unlink
      end
  end

模型

 class Photo < ActiveRecord::Base
  has_attached_file :photo, styles: { thumbnail: "150x200#"}, default_style: :thumbnail
end

和错误:

NoMethodError at /photos
===================================
> undefined method `stringify_keys' for #<String:0xb46dba14>
activerecord (4.0.0) lib/active_record/attribute_assignment.rb, line 17

解决方法:

好吧,我想我得到了一些东西.

存在两个潜在问题:

>您使用画布创建的文件可能不正确
>您的@ post.save函数可能不正确

我不知道画布的内容….因此,我将使用@ post.save为您提供最佳拍摄:

 def create
    decoded_file = Base64.decode64(params[:photo])
      begin
        file = Tempfile.new(['test', '.jpg']) 
        file.binmode
        file.write decoded_file
        file.close

        params[:photo] = file

        @photo.new(photo_params)
        if @photo.save
          render :json => {:message => "Successfully uploaded the profile picture."}
        else
          render :json => {:message => "Failed to upload image"}
        end
      ensure
        file.unlink
      end
  end

  private
  def photo_params
      params.permit(:photo)
  end

标签:canvas,paperclip,javascript,ruby-on-rails
来源: https://codeday.me/bug/20191122/2061319.html