Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip with base64 : undefined method `stringify_keys' for #<String:0xb46dba14>

Please help me... I use paperclip to upload 1 picture in the canvas tag (base64) to aws-s3.

My Controller

    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

Model

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

And errors :

NoMethodError at /photos
===================================
> undefined method `stringify_keys' for #<String:0xb46dba14>
activerecord (4.0.0) lib/active_record/attribute_assignment.rb, line 17
like image 471
JohnEvans Avatar asked Jan 30 '26 01:01

JohnEvans


1 Answers

Okay, I think I got something.

There are 2 potential problems:

  1. Your creation of a file (with canvas) might be incorrect
  2. Your @post.save function might be incorrect

I don't know about the canvas stuff.... so I'm going to give you my best shot with the @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
like image 163
Richard Peck Avatar answered Feb 01 '26 16:02

Richard Peck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!