How to upload image in a rails json api using paperclip?
<p>Hi I'm building a json api on rails. I have a model class Product that has a image associated with it.
<strong>Here's my Products Controller</strong></p>

<pre><code>def create
         params[:product][:image] = parse_image_data(params[:product][:image]) if params[:product][:image]
         product = Product.new(product_params)
         product.image = params[:product][:image]
         if product.save
            render json: product, status: 201
        else
            render json: {error: product.errors}, status: 422
        end
    end
private
    def product_params
        params.require(:product).permit(:title,:description,:published,:product_type, :user_id, :image, address_attributes:[:address, :city_id])
    end

    def parse_image_data(image_data)
    @tempfile = Tempfile.new('item_image')
    @tempfile.binmode
    @tempfile.write Base64.decode64(image_data[:content])
    @tempfile.rewind

    uploaded_file = ActionDispatch::Http::UploadedFile.new(
      tempfile: @tempfile,
      filename: image_data[:filename]
    )

   uploaded_file.content_type = image_data[:content_type]
    uploaded_file
  end

  def clean_tempfile
    if @tempfile
      @tempfile.close
      @tempfile.unlink
    end
  end
</code></pre>

<p><strong>The Product Model :</strong></p>

<pre><code>class Product &lt; ActiveRecord::Base
    belongs_to :user
    has_one :address
    has_attached_file :image, styles: { thumb: ["64x64#", :jpg],
                                        original: ['500x500&gt;', :jpg] },
                              convert_options: { thumb: "-quality 75 -strip",
                                     original: "-quality 85 -strip" }

    validates :title, :description, :user_id, presence: true
    validates :product_type, numericality:{:greater_than =&gt; 0, :less_than_or_equal_to =&gt; 2}, presence: true
    accepts_nested_attributes_for :address
    validates_attachment :image,
                     content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] },
                     size: { in: 0..500.kilobytes }
end
</code></pre>

<p>I'm sending the post request using <strong>postman</strong> with the following request payload :</p>

<pre><code>{
    "product": {
        "title":"Lumial 940",
        "description": "A black coloured phone found in Hudda Metro Station",
        "published": "true",
        "product_type":"2",
        "user_id":"1",
        "address_attributes":{
        "address": "Flat 16, Sharan Apartments, South City 1",
        "city_id": "2"
    },
    "image":{
        "filename": "minka.jpg",
         "content": "BASE64 STRING",
      "content_type": "image/jpeg"
    }
    }
}
</code></pre>

<p>But while creating, the Product is being created with the image properties but the image_path attribute is saved as null in the database. I can see the image being written in the public directory under my rails application. How can I get to save the image_path as well?</p>

<p>Can someone help me how to solve this?</p>