title: Rails API Paperclip. Uploading image converting it to base 64 and saving it and retrieving it
body: <p>Hello i am creating an api using Ruby on Rails.</p>
  
  <p>I am using paperclip gem.</p>
  
  <p>I have a <code>profile</code> model that has an avatar. How do i go about allowing a user to upload an avatar? Currently im quite lost. Problem is i can get this architecture to work. I am quite beginner so any help would be great. Im really unsure about how to get the base64 converted image and store the image in the database. </p>
  
  <p>My <code>Profile</code> Model:</p>
  
  <pre><code>class Profile &lt; ActiveRecord::Base
    belongs_to :user
    validates :user, presence: true
  
    before_validation :set_image
  
    has_attached_file :avatar, styles: {thumb: "100x100&gt;" }, default_url: "/images/:style/missing.png"
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
  
    #image_json is the image in base64 string
  
    def set_image
      StringIO.open(Base64.decode64(image_json)) do |data|
        data.class.class_eval { attr_accessor :original_filename, :content_type }
        data.original_filename = "file.gif"
        data.content_type = "image/gif"
        self.avatar = data
      end
    end
  end
  </code></pre>
  
  <p>Here is my update action: Currently a profile no avatar and im trying to update it with one.</p>
  
  <pre><code>def update
    if @profile.update(profile_params)
      render json: @profile, status: :ok
    else
      render json: json_errors(@profile.errors), status: :unprocessable_entity
    end
  end
  </code></pre>
  
  <p>Schema</p>
  
  <pre><code>  create_table "profiles", force: :cascade do |t|
      t.integer  "user_id"
      t.date     "birthday"
      t.text     "bio"
      t.string   "phone"
      t.string   "address_line_1"
      t.string   "address_line_2"
      t.string   "suburb"
      t.string   "state"
      t.string   "postcode"
      t.string   "country_code"
      t.string   "first_name"
      t.string   "last_name"
      t.string   "avatar_file_name"
      t.string   "avatar_content_type"
      t.integer  "avatar_file_size"
      t.datetime "avatar_updated_at"
    end
  </code></pre>